;; open Assert (* command to instruct testing harness to stop after the *) (* first failing test case. *) ;; stop_on_failure () (*** Lists and tuples together ***) (* convert a list of tuples to a string. *) let rec string_of_isl (x : (int * string) list) : string = failwith "string_of_isl: undefined" (* let test () : bool = string_of_isl [(1,"a");(3,"b")] = "(1,a)::(3,b)::[]" ;; run_test "string_of_isl" test *) (* --- assoc --- *) (* look up the string associated with an int in a list of tuples *) let rec assoc (x: (int * string) list) (v:int) : string = begin match x with | [] -> "not found" | (y,z) :: tl -> if v = y then z else assoc tl v end let test () : bool = assoc [] 1 = "not found" ;; run_test "assoc []" test let test () : bool = assoc [(1,"a");(2,"b")] 1 = "a" ;; run_test "assoc 1" test let test () : bool = assoc [(1,"a");(2,"b")] 2 = "b" ;; run_test "assoc 2" test let test () : bool = assoc [(1,"a");(2,"b")] 3 = "not found" ;; run_test "assoc 3" test (* --- zip --- *) (* Combine two lists together into a list of tuples *) let rec zip (x1:int list) (x2:string list) : (int * string) list = failwith "zip: unimplemented" let test () : bool = zip [] [] = [] ;; run_test "zip empty" test let test () : bool = zip [1;2;3] ["a";"b";"c"] = [(1,"a");(2,"b");(3,"c")] ;; run_test "zip three" test (******* More list examples: The rest of the problems in this file are challenge problems, designed to give you more experience with lists. ********) (* --- converting lists to strings --- *) (* To help with debugging, first implement some simple *) (* string conversion functions *) let rec string_of_il (l:int list) : string = failwith "string_of_il: unimplemented" let test () : bool = string_of_il [1;2;3] = "1::2::3::[]" ;; run_test "string_of_il" test let rec string_of_ill (l:int list list) : string = failwith "string_of_ill: unimplemented" let test () : bool = string_of_ill [[1;2;3];[2;3]] = "(1::2::3::[])::(2::3::[])::[]" ;; run_test "string_of_ill" test (* --- challenge: tails --- *) let rec tails (l:int list) : int list list = failwith "tails: undefined" let test () : bool = (tails []) = [ [] ] ;; run_test "tails nil" test let test () : bool = (tails [1;2;3]) = [[1;2;3]; [2;3]; [3]; []] ;; run_test "tails [1;2;3]" test (*--- challenge: inits ----*) let rec inits (l:int list) : int list list = failwith "inits: undefined" let test () : bool = (inits []) = [[]] ;; run_test "inits nil" test let test () : bool = (inits [1]) = [[]; [1]] ;; run_test "inits [1]" test let test () : bool = (inits [1;2]) = [[]; [1]; [1;2]] ;; run_test "inits [1;2]" test let test () : bool = (inits [1;2;3]) = [[]; [1]; [1;2]; [1;2;3]] ;; run_test "inits [1;2;3]" test (*** All rotations ***) let rotations (l:int list) : int list list = failwith "undefined" let test () : bool = (rotations []) = [] ;; run_test "rotations []" test let test () : bool = (rotations [1]) = [[1]] ;; run_test "rotations [1]" test let test () : bool = (rotations [1;2]) = [[1;2];[2;1]] ;; run_test "rotations [1;2]" test let test () : bool = (rotations [1;2;3]) = [[1;2;3]; [2;3;1]; [3;1;2]] ;; run_test "rotations [1;2;3]" test ;; print_string "lists.ml ran to completion"