1
votes

I have the follwing function that is suppose to return the value of a card. I'm not sure why the case Num => Num is giving the following error:

Error: Types of rules don't agree, Earlier rules rank->int this rule rank->int->rank

Why would Num=>Num have the return type rank->int->rank

 datatype suit = Clubs | Diamonds | Hearts | Spades
 datatype rank = Jack | Queen | King | Ace | Num of int 
 type card = suit * rank


 fun card_value (suit, rank)=
 case rank of
    Ace =>11
     | Jack =>10
     | King =>10
     | Queen =>10
     | Num => Num ;


card_value(Clubs,Ace); //calling function
1
Coursera homework? ;-)Frank Schmitt

1 Answers

10
votes

The error message is indicative. You need to have the same return type for all patterns.

fun card_value (suit, rank) =
  case rank of
      Ace => 14
    | King => 13
    | Queen => 12
    | Jack => 11
    | Num i => i

So you have to specify the correct constructor Num i (not Num only) and return i as an int