gpt4 book ai didi

haskell - 不在范围内 : data constructor?

转载 作者:行者123 更新时间:2023-12-03 23:15:43 26 4
gpt4 key购买 nike

我是haskell的新手。我遇到了这个问题:

Assignment12.hs:5:12:不在范围内:数据构造函数“Suit”

Assignment12.hs:5:18:不在范围内:数据构造函数“Rank”

Assignment12.hs:6:11:不在范围内:数据构造函数“Rank”

Assignment12.hs:7:11:不在范围内:数据构造函数“Rank”

Assignment12.hs:8:11:不在范围内:数据构造函数“否则”
失败,加载模块:无。

这是我的代码:

data Suit = Clubs | Diamonds | Hearts | Spades deriving (Show, Eq)
data Rank = Jack | Queen | King | Ace | Num Int deriving (Show, Eq)
type Card = (Suit, Rank)
cardValue :: Card -> Int
cardValue (Suit, Rank)
| Rank == Ace = 11
| Rank == Ace = 11
| Rank == Jack = 10
| Otherwise = Num

我真的很感谢你的帮助。谢谢

最佳答案

在 haskell 中,变量名必须以小写字母开头。任何大写字母都会被编译器解释为 Data Constructor这就是您收到该错误的原因。

当您定义 cardRank变量 (suit, rank)必须以小写字母开头,您的代码才能编译。

这应该工作

data Suit = Clubs | Diamonds | Hearts | Spades deriving (Show, Eq)
data Rank = Jack | Queen | King | Ace | Num Int deriving (Show, Eq)
type Card = (Suit, Rank)

cardValue :: Card -> Int
cardValue (suit, rank)
| rank == Ace = 11
| rank == Ace = 11
| rank == Jack = 10
cardValue (suit, Num x) = x

关于haskell - 不在范围内 : data constructor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28381055/

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