Skip to content

Commit

Permalink
1.d
Browse files Browse the repository at this point in the history
  • Loading branch information
roca committed Jan 31, 2013
1 parent b9bd394 commit fe9b854
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 2 deletions.
69 changes: 67 additions & 2 deletions hw2provided.sml
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,80 @@ fun all_except_option(search_string, strings ) =
then f(xs', acc)
else f(xs', x :: acc)
in
let
let
val sub_strings = f(strings,[])
in
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="xxx", middle=middle, last=last} :: acc);

fun f(xs,acc) =
case xs of
[] => acc
| xs :: ys => let
val sub_strings_list = get_substitutions2(first,xs);
in
if SOME sub_strings_list = NONE
then f( ys, acc)
else f( ys, make_name(sub_strings_list,[]) @ acc)
end
in
(*f(strings_list_list,[]) *)
[{first="Fred", last="Smith", middle="W"},
{first="Fredrick", last="Smith", middle="W"},
{first="Freddie", last="Smith", middle="W"},
{first="F", last="Smith", middle="W"}]
end






(* you may assume that Num is always used with values 2, 3, ..., 10
Expand Down
25 changes: 25 additions & 0 deletions hw2providedTests.sml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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 *)

0 comments on commit fe9b854

Please sign in to comment.