不在范围内:数据构造函数?

时间:2015-02-07 10:29:46

标签: 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

我非常感谢你的帮助。感谢

1 个答案:

答案 0 :(得分:6)

在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