7

I am new to haskell. I am getting this problem :

Assignment12.hs:5:12: Not in scope: data constructor `Suit'

Assignment12.hs:5:18: Not in scope: data constructor `Rank'

Assignment12.hs:6:11: Not in scope: data constructor `Rank'

Assignment12.hs:7:11: Not in scope: data constructor `Rank'

Assignment12.hs:8:11: Not in scope: data constructor `Otherwise' Failed, modules loaded: none.

This is my code :

data Suit = Clubs | Diamonds | Hearts | Spades deriving (Show, Eq)
data Rank = Jack | Queen | King | Ace | Num Int deriving (Show, Eq)
type Card = (Suit, Rank)
cardValue :: Card -> Int
cardValue (Suit, Rank)
     | Rank == Ace = 11
     | Rank == Ace = 11
     | Rank == Jack = 10
     | Otherwise = Num

I really appreciate your help. Thanks

Achref
  • 89
  • 1
  • 2
  • 6
  • 1
    You have `Ace` twice and aren't handling `King` or `Queen` - it looks like `Ace` should have value 13 then `King`, `Queen`, `Jack`? – Lee Feb 07 '15 at 10:40

1 Answers1

20

In haskell variable names must start with a lowercase letter. Anything that is uppercase is interpreted by the compiler as a Data Constructor which is why you are getting that error.

When you define cardRank the variables (suit, rank) must start with lowercase letters for your code to compile.

This should work

data Suit = Clubs | Diamonds | Hearts | Spades deriving (Show, Eq)
data Rank = Jack | Queen | King | Ace | Num Int deriving (Show, Eq)
type Card = (Suit, Rank)

cardValue :: Card -> Int
cardValue (suit, rank)
     | rank == Ace = 11
     | rank == Ace = 11
     | rank == Jack = 10
cardValue (suit, Num x) = x
Simon Gibbons
  • 6,969
  • 1
  • 21
  • 34