I am attending a Coursera class, and I am trying to do my homeworks.
We have to write an SML program that takes a list of cards (caracterised by their suit and rank) and returns true if they all have the same color and false otherwise.
Here is my code (i can’t figure why its false, but i am quit a noob in programming):
datatype suit = Clubs | Diamonds | Hearts | Spades
datatype rank = Jack | Queen | King | Ace | Num of int
type card = suit * rank
datatype color = Red | Black
datatype move = Discard of card | Draw
fun card_color (c) = case c of
(Hearts,_) => Red
|(Diamonds,_) => Red
|(_,_) => Black
fun all_same_color (cs) = case cs of
[] => false
|x::[] => true
|x::y::[] => if card_color (x) = card_color (y) then true
|x::y::xs => if card_color(x)=card_color(y) then all_same_color(xs)
else false
You cannot have an
if ... then ...without anelse ...case.You attempt to do this here:
Remember,
if ... then ... else ...is an expression, and thus needs a value whether or not the condition is true or false.In addition, if you get an
if-then-else, where either thethenorelseparts aretrueorfalsedirectly, you can write it more succinctly. For instance,is the same as saying