gpt4 book ai didi

haskell - 使用列表推导式在 Haskell 中完成一整套

转载 作者:行者123 更新时间:2023-12-02 17:04:32 24 4
gpt4 key购买 nike

<分区>

我对 Haskell(和编程)还很陌生。我的任务是定义一个函数来返回一副完整的牌(又名 52 张牌)。尝试将我的思考过程包含在代码旁边的注释中。

-- 我定义的值

data Suit = Club | Diamond | Heart | Spade  deriving (Show, Enum)

data Value = Two | Three | Four | Five | Six | Seven
| Eight | Nine | Ten | Jack | Queen
| King | Ace deriving (Show, Enum)

type Card = (Suit, Value) -- A card must have a suit and a value
type Deck = [Card] -- A deck consists of a list of cards

fullDeck :: Deck -- My function is supposed to consist of a deck
fullDeck = [(suit, value) | suit <- [Club..Spade], value <- [Two..Ace]] -- Tried my luck using ''list comprehensions''. Is it necessary to type [Club..Spade] or does it work for just [Club..] as well?

我的代码无法加载。我得到的错误:

beginner.hs:11:62: error:
A section must be enclosed in parentheses thus: (Two.. Ace)
|
11 | fullDeck = [(suit, value) | suit <- [Club..Spade], value <- [Two..Ace]]

无论我如何尝试解决它,都会出现一些新的错误,所以我显然正在做一些我无法定位的重大失败。

另外我很好奇如何确保我的套牌中只有 52 张牌,因为目前我的列表包含无限数量?

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