Skip to content

Commit

Permalink
2.c
Browse files Browse the repository at this point in the history
  • Loading branch information
roca committed Feb 1, 2013
1 parent 7d52eb0 commit 8398639
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
47 changes: 40 additions & 7 deletions hw2provided.sml
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,52 @@ exception IllegalMove

(* put your solutions for problem 2 here *)

exception NoCardsFound

fun card_color(card) =
case card of
(Spades , _) => Black
|(Clubs , _) => Black
(Spades , _) => Black
|(Clubs , _) => Black
|(Diamonds , _) => Red
|(Hearts , _) => Red
|(Hearts , _) => Red

fun card_value(card) =
case card of
(_ , Jack) => 10
(_ , Jack) => 10
|(_ , Queen) => 10
|(_ , King) => 10
|(_ , Ace) => 11
| (_,Num x) => x
|(_ , King) => 10
|(_ , Ace) => 11
|(_ , Num x) => x


fun same(c1 , c2 ) =
c1 = c2


fun all_cards_except_option(pick, cards ) =
let fun f (xs,acc) =
case xs of
[] => acc
| x :: xs' => if same(pick,x)
then f(xs', acc)
else f(xs', x :: acc)
in
let
val cards_left = f(cards,[])
in
if length(cards_left) = length(cards)
then NONE
else SOME cards_left
end
end


fun remove_card( deck , pick , ex) =
let
val cards_left = all_cards_except_option(pick,deck)
in
if cards_left = NONE
then raise ex
else getOpt(cards_left,[])
end

8 changes: 8 additions & 0 deletions hw2providedTests.sml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,11 @@ card_value(Spades,Num 3) = 3;
card_value(Clubs,Jack) = 10;
card_value(Diamonds,Queen) = 10;
card_value(Hearts,King) = 10;

(* 2.c *)
remove_card([(Spades,Ace),(Spades,Jack)],(Clubs,Queen),NoCardsFound) = NoCardsFound;
remove_card([(Spades,Ace),(Spades,Jack)],(Spades,Queen),NoCardsFound) = NoCardsFound;
remove_card([(Spades,Ace),(Spades,Jack)],(Spades,Ace),NoCardsFound) = [(Spades,Jack)];



0 comments on commit 8398639

Please sign in to comment.