0
votes

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]]
1
[x | x <- [2..10]] returns a list, so in that last line n is of type [Integer]. You can't really restrict the range of n on the type level. You'll probably just want to check it before it even gets added to NumericFarley Knight

1 Answers

6
votes

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 ...