简体   繁体   English

Haskell无法将期望的类型“ Integer”与实际类型“ [Integer]”进行匹配

[英]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.

相关问题 无法将期望的类型[a]与实际类型“整数-&gt; [整数]”匹配 - Couldn't match expected type [a] with actual type `Integer -> [Integer]' 无法将预期类型“ [Integer]”与实际类型“ Integer”匹配 - Couldn't match expected type ‘[Integer]’ with actual type ‘Integer’ Haskell:无法预期实际类型&#39;Int&#39;的&#39;Integer&#39;类型 - Haskell: Couldn't expected type 'Integer' with actual type 'Int' Haskell:无法将期望的类型“ IO t0”与实际类型“ Integer”匹配 - Haskell: Couldn't match expected type 'IO t0' with actual type 'Integer' Haskell powerset函数-如何避免无法将期望的类型“ IO t0”与实际类型“ [[Integer]]”匹配 - Haskell powerset function - How to avoid Couldn't match expected type `IO t0' with actual type `[[Integer]]' 无法将预期的类型&#39;Integer-&gt; t&#39;与实际类型匹配 - Couldn't match expected type 'Integer -> t' with actual type 无法将预期类型&#39;Integer - &gt; t&#39;与实际类型&#39;Bool&#39;匹配 - Couldn't match expected type ‘Integer -> t’ with actual type ‘Bool’ 无法将预期类型`a`与实际类型`Integer`相匹配 - Couldn't match expected type `a` with actual type `Integer` 无法将预期类型“Bool”与实际类型“[[Integer]] 匹配 - Couldn't match expected type `Bool' with actual type `[[Integer]] 无法将预期类型“Int”与实际类型“Integer”匹配 - Couldn't match expected type `Int' with actual type `Integer'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM