Skip to content

Commit

Permalink
Done with assignment 2
Browse files Browse the repository at this point in the history
  • Loading branch information
roca committed Feb 5, 2013
1 parent 8398639 commit 6f76f2c
Show file tree
Hide file tree
Showing 3 changed files with 224 additions and 3 deletions.
27 changes: 27 additions & 0 deletions hw2provided.sml
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,30 @@ fun remove_card( deck , pick , ex) =
else getOpt(cards_left,[])
end

fun all_same_color( cards) =
case cards of
[] => true
| x::[] => true
| first :: (second :: rest) => (card_color(first) = card_color(second) andalso all_same_color(second :: rest))




fun sum_cards( cards) =
let fun f(xs,acc) =
case xs of
[] => acc
| xs :: ys => f(ys, card_value(xs) + acc)
in
f(cards,0)
end


fun score(cards,goal) =
let
val sum = sum_cards(cards);
in
if sum <= goal
then goal - sum
else sum - goal
end
174 changes: 174 additions & 0 deletions hw2provided.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
(* Dan Grossman, Coursera PL, HW2 Provided Code *)

(* if you use this function to compare two strings (returns true if the same
string), then you avoid several of the functions in problem 1 having
polymorphic types that may be confusing *)
fun same_string(s1 : string, s2 : string) =
s1 = s2

(* put your solutions for problem 1 here *)


fun all_except_option(search_string, strings ) =
let fun f (xs,acc) =
case xs of
[] => acc
| x :: xs' => if same_string(search_string,x)
then f(xs', acc)
else f(xs', x :: acc)
in
let
val sub_strings = f(strings,[])
in
if length(sub_strings) = length(strings)
then NONE
else SOME sub_strings
end
end

fun get_substitutions1( strings_list_list, search_string) =
let fun f(xs,acc) =
case xs of
[] => acc
| xs :: ys => let
val sub_strings_list = all_except_option(search_string,xs);
in
if sub_strings_list = NONE
then f( ys, acc)
else f( ys, getOpt(sub_strings_list,[]) @ acc)
end
in
f(strings_list_list,[])
end




fun get_substitutions2( strings_list_list, search_string) =
let fun f(xs,acc) =
case xs of
[] => acc
| xs :: ys => let
val sub_strings_list = all_except_option(search_string,xs);
in
if sub_strings_list = NONE
then f( ys, acc)
else f( ys, getOpt(sub_strings_list,[]) @ acc)
end
in
f(strings_list_list,[])
end


fun similar_names( strings_list_list, {first,middle,last}) =
let
fun make_name(xs,acc) =
case xs of
[] => acc
| x :: xs' => make_name(xs',{first=x, middle=middle, last=last} :: acc);
fun f(xs,acc) =
case xs of
[] => acc
| xs :: ys => let
val sub_strings_list = get_substitutions2(xs,first);
in
if SOME sub_strings_list = NONE
then f( ys, acc)
else f( ys, acc @ make_name(sub_strings_list,[]))
end
in
f([strings_list_list],[{first=first,middle=middle,last=last}])
end




(* you may assume that Num is always used with values 2, 3, ..., 10
though it will not really come up *)
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

(* put your solutions for problem 2 here *)

exception NoCardsFound

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

fun card_value(card) =
case card of
(_ , Jack) => 10
|(_ , Queen) => 10
|(_ , 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

fun all_same_color( cards) =
case cards of
[] => true
| x::[] => true
| first :: (second :: rest) => (card_color(first) = card_color(second) andalso all_same_color(second :: rest))




fun sum_cards( cards) =
let fun f(xs,acc) =
case xs of
[] => acc
| xs :: ys => f(ys, card_value(xs) + acc)
in
f(cards,0)
end


fun score(cards,goal) =
let
val sum = sum_cards(cards);
in
if sum <= goal
then goal - sum
else sum - goal
end
26 changes: 23 additions & 3 deletions hw2providedTests.sml
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,29 @@ 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)];
val c1 = (Spades,Ace);
val c2 = (Spades,Jack);
val c3 = (Clubs,Queen);
val c4 = (Spades,Queen);

val y = remove_card([c1,c2],c3,NoCardsFound) handle NoCardsFound => [c1,c2];
val x = remove_card([c1,c2],c4,NoCardsFound) handle NoCardsFound => [c1,c2];

remove_card([c1,c2],c1,NoCardsFound) = [c2];

(* 2.d *)

all_same_color([(Spades,Ace),(Clubs,Ace),(Clubs,King)]) = true;
all_same_color([(Spades,Ace),(Diamonds,Ace)]) = false;


(* 2.e *)

sum_cards([c1,c2]) = 21;
sum_cards([(Spades,Num 9),(Clubs,Num 3),(Spades,King)]) = 22;

(* 2.f *)

score([c1,c2],5) = 21 - 5;
score([(Spades,Num 9),(Clubs,Num 3),(Spades,King)],25) = 25 - 22;

0 comments on commit 6f76f2c

Please sign in to comment.