Haskell迭代4个元素 [英] Haskell iterate lists by 4 elem

查看:113
本文介绍了Haskell迭代4个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何迭代四个元素的列表,然后将这四张牌添加到一个元组中?

基本上这个功能可以接受被杀的西装,按顺序玩的牌和先玩的玩家。

那场比赛是成对比赛的,所以它返回一个元组,第一对夫妇赢得了卡牌,第二对夫妇赢得了卡牌。

  winnedCards :: Suit-> [卡片]  - > Int  - > ([Card],[Card])
winnedCards [] _ =([],[])
winnedCards(西装类型)cardsPlayed positionFirstPlayPerson
| snd(xs)== 3 =(取4(cardsPlayed),[])
| snd(xs)== 1 =(take 4(cardsPlayed),[])
|否则=([],取2(cardsPlayed))
其中xs = whoHasWon(取4(cardsPlayed))(西装类型)



whoWasWon返回获胜的玩家的位置。我认为我必须回答它,因为我必须迭代4乘4,然后在每次迭代后将结果添加到元组中。

解决方案

正如Willem Van Onsem在评论中提到的那样,你的功能似乎完成了太多的工作。如果你试图将一个列表分成四元组,请与逻辑的其余部分分开进行。

  toFourTuple :: [a]  - > [(a,a,a,a)] 
toFourTuple [] = []
toFourTuple(a:b:c:d:rest)=(a,b,c,d):toFourTuple rest
toFourTuple _ =错误列表不能被4整除
- 应如何处理?

其余部分,知道的Monoid实例可能很有用,幺半群a => (x,y)(x',y')=(x,a) implements mappend <> x',y<> y'),所以如果你生成(左侧,右侧)子列表的列表,你可以 mconcat 它们在一起。

  xs = [([1] ,[3])
,([2,4],[5])
,([6],[7,9,11,13])
]
mconcat xs =([1,2,4,6],[3,5,7,9,11,13])

当然这也是递增的。

   -  |将[Int]分隔成([odds] ,[evens])
splitByMod2 :: [Int] - > ([Int],[Int])
splitByMod2 [] =([],[])
splitByMod2(x:xs)
|奇数x =([x],[])`mappend` splitByMod2 xs
|甚至x =([],[x])`mappend` splitByMod2 xs
|否则=错误这不可能发生


How I can iterate a list of elements four by four and then add these four card to a tuple?.

Basically this a function that receives the Suit that it was to be killed, the cards played by order and the player who has played first.

That game is played by pairs, so it returns a tuple with the cards won for the first couple and the cards won for the second couple.

winnedCards:: Suit-> [Card] -> Int -> ([Card],[Card])
winnedCards [] _ = ([],[])
winnedCards (Suit type) cardsPlayed positionFirstPlayPerson
 | snd(xs) == 3 = (take 4 (cardsPlayed),[])
 | snd(xs) == 1 = (take 4 (cardsPlayed),[])
 | otherwise = ([],take 2 (cardsPlayed))
 where xs = whoHasWon (take 4 (cardsPlayed)) (Suit type)

whoHasWon returns the position of the player who has won. I think I have to it recursevily because I have to iterate 4 by 4, and then add the result to the tuple after each iteration.

解决方案

As Willem Van Onsem mentions in the comments, your function appears to do entirely too much work. If you're trying to split a list into groups of four-tuples, do that separately from the rest of the logic.

toFourTuple :: [a] -> [(a, a, a, a)]
toFourTuple [] = []
toFourTuple (a:b:c:d:rest) = (a, b, c, d) : toFourTuple rest
toFourTuple _  = error "list not divisible by four"
                 -- how should this be handled?

For the rest, it might be useful to know that the Monoid instance of Monoid a => (a, a) implements mappend as mappend (x, y) (x', y') = (x <> x', y <> y'), so if you generate a list of (leftside, rightside) sublists, you can mconcat them together.

xs = [ ([1]   , [3])
     , ([2, 4], [5])
     , ([6]   , [7, 9, 11, 13])
     ]
mconcat xs = ([1,2,4,6],[3,5,7,9,11,13])

Of course this works incrementally too.

-- |Separates [Int] into ([odds], [evens])
splitByMod2 :: [Int] -> ([Int], [Int])
splitByMod2 [] = ([], [])
splitByMod2 (x:xs)
  | odd x  = ([x], []) `mappend` splitByMod2 xs
  | even x = ([], [x]) `mappend` splitByMod2 xs
  | otherwise = error "This cannot happen"

这篇关于Haskell迭代4个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆