Question: Problems 5-9 use these datatypes: datatype suit = Clubs | Diamonds | Hearts | Spades datatype rank = Jack | Queen | King | Ace
Problems 5-9 use these datatypes:
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
exception IllegalMove
In HTML, write a function card_color which takes an argument of type card and returns its color (spades and clubs are black, diamonds and hearts are red).
; c is a tuple but we have to specify the type because we
; use the #1 operator
fun card_color1 (c : card) =
if (#1 c) = Clubs orelse (#1 c) = Spades
then Black
else Red
; a single tuple is passed in as the argument. we don't have
; to specify types because we don't use #1
fun card_color2 (s:suit, r:rank) =
if s = Clubs orelse s = Spades
then Black
else Red
; ML infers that c is a tuple and infers the types of the tuple
; members because we use a case statement
(Hearts, Num 10)
(Spades, Ace)
(Diamonds, Jack)
fun card_color (c:card) =
case c of
(Clubs, _) => Black
| (Spades, _) => Black
| _ => Red
In HTML, write a function card_value, which takes a card and returns its value (numbered cards have their number as the value, aces are 11, everything else is 10).
In HTML, write a function remove_card, which takes a list of cards cs, a card c, and an exception e. It returns a list that has all the elements of cs except c. If c is in the list more than once, remove only the first one. If c is not in the list, raise the exception e. You can compare cards with =.
In HTML, write a function all_same_color, which takes a list of cards and returns true if all the cards in the list are the same color.
In HTML write a function sum_cards, which takes a list of cards and returns the sum of their values.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
