[英]Haskell Couldn't match expected type `Integer' with actual type `[Integer]'
Here is my code how can I say where n is int between 2..10 这是我的代码,我怎么能说n在2..10之间的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]]
I suggest you use pattern matching instead of guards: 我建议您使用模式匹配代替警卫:
valueRank :: Rank -> Integer
valueRank Jack = 10
valueRank King = 10
valueRank Queen = 10
valueRank Ace = 10
valueRank (Numeric n) = n
If you want to make sure that a Numeric can't be created with a value outside of certain range, then when making a rank you should use a smart constructor
that validates this property: 如果要确保不能使用超出特定范围的值来创建数值,那么在进行排名时,应使用一个
smart constructor
来验证此属性:
makeRank n
| 1 <= n <= 13 = ...
| otherwise = error ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.