diff --git a/sml/week2/card.sml b/sml/week2/card.sml index 947b4f1..9983c8b 100644 --- a/sml/week2/card.sml +++ b/sml/week2/card.sml @@ -36,6 +36,7 @@ val remove_card: remove_card = fn (cards, to_remove, exp) => | (false, _) => raise exp end +(* not tail recursive *) type all_same_color = card list -> bool val rec all_same_color: all_same_color = fn cards => case cards of @@ -48,6 +49,9 @@ type sum_cards = card list -> int val sum_cards: sum_cards = fn cards => cards |> fold (fn card => fn sum => card_value card + sum) 0 +(* even shorter via partial application :) *) +val sum_cards: sum_cards = fold (fn card => fn sum => card_value card + sum) 0 + type score = card list * int -> int val score: score = fn (cards, goal) => let diff --git a/sml/week2/card_tests.sml b/sml/week2/card_tests.sml index 42da0f1..0b24359 100644 --- a/sml/week2/card_tests.sml +++ b/sml/week2/card_tests.sml @@ -2,28 +2,34 @@ use "test.sml"; use "card.sml"; val () = - assert (card_color (Spade, King) = Black) - "card_color: returns right color" + assert + $ card_color (Spade, King) = Black + $ "card_color: returns right color" val () = - assert (card_color (Heart, King) = Red) - "card_color: returns right color x2" + assert + $ card_color (Heart, King) = Red + $ "card_color: returns right color x2" val () = - assert (card_value (Heart, Num 5) = 5) - "card_value: number card" + assert + $ card_value (Heart, Num 5) = 5 + $ "card_value: number card" val () = - assert (card_value (Heart, Ace) = 11) - "card_value: ace is 11" + assert + $ card_value (Heart, Ace) = 11 + $ "card_value: ace is 11" val () = - assert (card_value (Heart, Jack) = 10) - "card_value: jack is 10" + assert + $ card_value (Heart, Jack) = 10 + $ "card_value: jack is 10" val () = - assert (remove_card ([(Heart, Jack)], (Heart, Jack), IllegalMove) = []) - "remove_card: removes card" + assert + $ remove_card ([(Heart, Jack)], (Heart, Jack), IllegalMove) = [] + $ "remove_card: removes card" val () = let @@ -33,24 +39,21 @@ val () = val exp = IllegalMove in assert - (remove_card (deck, card, exp) = expected) - "remove_card: leaves duplicates" + $ remove_card (deck, card, exp) = expected + $ "remove_card: leaves duplicates" end -val _ = +val [] = let val deck = [] val card = (Heart, Jack) - val expected = IllegalMove + val expected = [] val exp = IllegalMove in remove_card (deck, card, exp) handle IllegalMove => - let - val () = assert true "remove_card: raises exception" - in - [] - end + assert true "remove_card: raises exception" + |> (fn _ => expected) end val () = @@ -201,12 +204,12 @@ val 0 = val deck = [(Club, Jack), (Club, Num 3), (Heart, Num 10), (Club, Jack)] val moves = [Discard (Club, Jack), Draw, Draw, Draw] val goal = 19 - val expected = 6 + val expected = 0 in officiate (deck, moves, goal) handle IllegalMove => assert true "officiate: raises exception on missing card" - |> (fn _ => 0) + |> (fn _ => expected) end val () = complete () \ No newline at end of file