-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
97 additions
and
72 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,22 @@ | ||
use "hw2provided.sml"; | ||
|
||
(* 1.a *) | ||
all_except_option( "x", ["x","y","z"]) = SOME(rev(["y","z"])); | ||
|
||
(* 1.b *) | ||
get_substitutions1([["Fred","Fredrick"],["Elizabeth","Betty"],["Freddie","Fred","F"]],"Fred") | ||
= rev ["Fredrick","Freddie","F"]; (* Should be true *) | ||
|
||
(* 1.c *) | ||
get_substitutions2([["Fred","Fredrick"],["Jeff","Jeffrey"],["Geoff","Jeff","Jeffrey"]],"Jeff") | ||
= ["Jeffrey","Geoff","Jeffrey"]; (* Should be true *) | ||
|
||
get_substitutions2([["Fred","Fredrick"],["Jeff","Jeffrey"],["Geoff","Jeff","Jeffrey"]],"Harry") | ||
= []; (* Should be true *) | ||
|
||
|
||
(* 1.d *) | ||
|
||
similar_names([["Fred","Fredrick"],["Elizabeth","Betty"],["Freddie","Fred","F"]],{first="Fred", middle="W", last="Smith"}) = | ||
[{first="Fred", last="Smith", middle="W"}, | ||
{first="Fredrick", last="Smith", middle="W"}, | ||
{first="Freddie", last="Smith", middle="W"}, | ||
{first="F", last="Smith", middle="W"}] ; (* Should be true *) | ||
|
||
|
||
(* 2.a *) | ||
card_color(Spades,Ace) = Black; | ||
card_color(Clubs,Ace) = Black; | ||
card_color(Diamonds,Ace) = Red; | ||
card_color(Hearts,Ace) = Red; | ||
|
||
|
||
(* 2.b *) | ||
card_value(Spades,Ace) = 11; | ||
card_value(Clubs,Ace) = 11; | ||
card_value(Diamonds,Ace) = 11; | ||
card_value(Hearts,Ace) = 11; | ||
|
||
card_value(Spades,Num 9) = 9; | ||
card_value(Spades,Num 3) = 3; | ||
card_value(Clubs,Jack) = 10; | ||
card_value(Diamonds,Queen) = 10; | ||
card_value(Hearts,King) = 10; | ||
|
||
(* 2.c *) | ||
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; | ||
|
||
(* Dan Grossman, Coursera PL, HW2 Provided Tests *) | ||
|
||
(* These are just two tests for problem 2; you will want more. | ||
Naturally these tests and your tests will use bindings defined | ||
in your solution, in particular the officiate function, | ||
so they will not type-check if officiate is not defined. | ||
*) | ||
|
||
fun provided_test1 () = (* correct behavior: raise IllegalMove *) | ||
let val cards = [(Clubs,Jack),(Spades,Num(8))] | ||
val moves = [Draw,Discard(Hearts,Jack)] | ||
in | ||
officiate(cards,moves,42) | ||
end | ||
|
||
fun provided_test2 () = (* correct behavior: return 3 *) | ||
let val cards = [(Clubs,Ace),(Spades,Ace),(Clubs,Ace),(Spades,Ace)] | ||
val moves = [Draw,Draw,Draw,Draw,Draw] | ||
in | ||
officiate(cards,moves,42) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
(* Coursera Programming Languages, Homework 3, Provided Code *) | ||
|
||
exception NoAnswer | ||
|
||
datatype pattern = Wildcard | ||
| Variable of string | ||
| UnitP | ||
| ConstP of int | ||
| TupleP of pattern list | ||
| ConstructorP of string * pattern | ||
|
||
datatype valu = Const of int | ||
| Unit | ||
| Tuple of valu list | ||
| Constructor of string * valu | ||
|
||
fun g f1 f2 p = | ||
let | ||
val r = g f1 f2 | ||
in | ||
case p of | ||
Wildcard => f1 () | ||
| Variable x => f2 x | ||
| TupleP ps => List.foldl (fn (p,i) => (r p) + i) 0 ps | ||
| ConstructorP(_,p) => r p | ||
| _ => 0 | ||
end | ||
|
||
(**** for the challenge problem only ****) | ||
|
||
datatype typ = Anything | ||
| UnitT | ||
| IntT | ||
| TupleT of typ list | ||
| Datatype of string | ||
|
||
(**** you can put all your code here ****) | ||
|
||
(* 1 *) | ||
|
||
fun only_capitals strings = | ||
List.filter (fn x => Char.isUpper(String.sub(x,0))) strings | ||
|
||
(* 2 *) | ||
|
||
fun longest_string1 strings = | ||
List.foldl ( fn (x,y) => if String.size(x) > String.size(y) then x else y ) "" strings | ||
|
||
(* 3 *) | ||
|
||
fun longest_string2 strings = | ||
List.foldl ( fn (x,y) => if String.size(x) >= String.size(y) then x else y ) "" strings | ||
|
||
|
||
(* 4 *) | ||
|
||
fun longest_string_helper(compare_function,strings) = | ||
List.foldl ( fn (x,y) => if compare_function(String.size(x),String.size(y)) then x else y ) "" strings | ||
|
||
|
||
fun longest_string3 strings = | ||
let | ||
val compare_function = fn (x,y) => x > y ; | ||
in | ||
longest_string_helper(compare_function,strings) | ||
end | ||
|
||
|
||
fun longest_string4 strings = | ||
let | ||
val compare_function = fn (x,y) => x >= y ; | ||
in | ||
longest_string_helper(compare_function,strings) | ||
end | ||
|