gpt4 book ai didi

Deal cards in Haskell(在哈斯克尔发牌)

转载 作者:bug小助手 更新时间:2023-10-25 21:41:32 24 4
gpt4 key购买 nike



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.)

(这甚至都不能编译。)


更多回答

What have you tried?

你都试了些什么?

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接受两个列表,如果其中一个是有限的,而另一个是无限的,那么一旦有限列表结束,输出就会停止。

优秀答案推荐

Solution turned out to be:

结果,解决方案是:


deal :: [String] -> [String] -> [(String,String)]
deal ps cs = zip cs (cycle ps)

更多回答

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com