I need to define a function
我需要定义一个函数
deal :: [String] -> [String] -> [(String,String)]
It needs to work like this:
它需要像这样工作:
deal ["Hercule","Ariadne"] ["Ace","Joker","Heart"] -- Outputs [("Ace","Hercule"),("Joker","Ariadne"),("Heart","Hercule")]
take 4 (deal ["a","b","c"] (map show [0..])) -- Outputs [("0","a"),("1","b"),("2","c"),("3","a")]
deal ("you":(repeat "me")) ["1","2","3","4"] -- Outputs [("1","you"),("2","me"),("3","me"),("4","me")]
Also, to be quite honest, this is an exercise; I am advised to use zip
and cycle
.
另外,老实说,这是一种练习;我被建议使用拉链和自行车。
I already tried to solve this for three and a half hours, yet failed to find the solution.
我已经试了三个半小时来解决这个问题,但还是没有找到解决方案。
Q: Would you give me a hint how to approach this problem?
问:你能给我一个提示,如何处理这个问题吗?
My best attempt
我最大的努力
deal :: [String] -> [String] -> [(String, String)]
deal ps cs = dealHelper ps cs ps cs []
where dealHelper [] [] _ _ result = result
dealHelper ps [] _ _ result = dealHelper ps (take (length ps) cs) result
dealHelper [] cs allps allcs result = dealHelper (take (length cs))
(That does not even compile.)
(这甚至都不能编译。)
更多回答
The advice points out the functions you need. cycle [a,b,c] = [a,b,c,a,b,c,a,b,c,...]
repeats the same list elements forever. You have two input lists: players and cards. Which one should be used repeatedly? Start by using cycle
on that. Also note that zip
takes two lists, and if one of them is finite but the other is infinite then the output will stop once the finite list is over.
该建议指出了您需要的功能。循环[a,b,c]=[a,b,c,...]永远重复相同的列表元素。您有两个输入列表:玩家和纸牌。哪一个应该重复使用?首先,对此使用循环。还要注意,Zip接受两个列表,如果其中一个是有限的,而另一个是无限的,那么一旦有限列表结束,输出就会停止。
我是一名优秀的程序员,十分优秀!