简体   繁体   中英

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

   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:

makeRank n
  | 1 <= n <= 13 = ...
  | otherwise = error ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM