funcprog: Minor improvements.

parent 67bd7fb4
......@@ -54,7 +54,10 @@ print "7.5: %b\n" ((=) (reduce (+) 0 [1;2;3;4;5]) 15);;
(* 6: isSublist l s that checks whether or not the argument list s is a sublist
* of the argument list l. *)
let rec isSublist l s = match (l, s) with
let isSublist l s =
(* Store the full search term *)
let full = s in
let rec sublistMatches l s = match (l, s) with
| _, [] -> true
| [], _ -> false
| h1::t1, h2::t2 ->
......@@ -63,14 +66,17 @@ let rec isSublist l s = match (l, s) with
(* and the next head of s matches the next head of l. *)
&& ( t1 = [] && t2 = [] || hd t1 = hd t2 )
(* recursively match the tails of s and l. *)
&& isSublist t1 t2
(* otherwise, continue with the tail of s. *)
|| isSublist t1 s
&& sublistMatches t1 t2
(* otherwise, continue with the tail of l and reset s. *)
|| sublistMatches t1 full
in sublistMatches l s
;;
print "7.6.1: %b\n" (isSublist [1;2;3;4] [3;4]);;
print "7.6.2: %b (%s)\n" (isSublist [1;2;3;4] [2;4]) "expected failure";;
print "7.6.3: %b\n" (isSublist [] []);;
print "7.6.3: %b (%s)\n" (isSublist [1;2;3;3;4] [2;3;4]) "expected failure";;
print "7.6.4: %b\n" (isSublist [1;2;3;3;4] [3;3;4]);;
print "7.6.5: %b\n" (isSublist [] []);;
(* 7. lookup l key that given a list l of (key, value) tuples and a key returns
* the value associated with that key. *)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment