这是我的代码,我怎么能说在2..10
之间n是int data Rank = Numeric Integer | Jack | Queen | King | Ace
deriving (Eq, Show)
valueRank :: Rank ->Integer
valueRank rank
|rank ==Jack = 10
|rank ==King = 10
|rank ==Queen = 10
|rank ==Ace = 10
|rank == Numeric n = n
where n =[x|x<-[2..10]]
答案 0 :(得分:6)
我建议你使用模式匹配而不是守卫:
valueRank :: Rank -> Integer
valueRank Jack = 10
valueRank King = 10
valueRank Queen = 10
valueRank Ace = 10
valueRank (Numeric n) = n
如果您想确保无法使用超出特定范围的值创建数字,那么在进行排名时,您应使用验证此属性的smart constructor
:
makeRank n
| 1 <= n <= 13 = ...
| otherwise = error ...