;; open Assert (* tails - computes the list of all suffixes of the input list *) let rec tails (l:int list) : int list list = begin match l with | [] -> [ [] ] | (h :: t) -> l :: (tails t) end let test () : bool = tails [1;2;3] = [[1;2;3]; [2;3]; [3]; []] ;; run_test "tails [1;2;3]" test let test () : bool = tails [2;3] = [ [2;3]; [3]; []] ;; run_test "tails [2;3]" test let test () : bool = tails [] = [ [] ] ;; run_test "tails []" test let rec prepend (h: int) (t : int list list) : int list list = begin match t with | [] -> [ ] | hd :: tl -> (h :: hd) :: (prepend h tl) end (* inits - computes the list of all prefixes of the input list *) let rec inits (l:int list) : int list list = begin match l with | [] -> [[]] | (h :: t) -> [] :: prepend h (inits t) end let test () : bool = inits [1;2;3] = [[]; [1]; [1;2]; [1;2;3]] ;; run_test "inits [1;2;3]" test let test () : bool = inits [2;3] = [[]; [2]; [2;3]] ;; run_test "inits [2;3]" test let test () : bool = inits [] = [[]] ;; run_test "inits []" test (* all_rotations - computes a list of all rotations of the input list *) let all_rotations (l:int list) : int list list = failwith "all_rotations: unimplemented" let test () : bool = all_rotations [1;2;3] = [[1;2;3]; [2;3;1]; [3;1;2]] ;; run_test "rotations [1;2;3]" test (* zip - combines two lists pairwise *) let rec zip (l1:int list) (l2:string list) : (int * string) list = failwith "zip: unimplemented" let test () : bool = zip [1;2;3] ["a";"b";"c"] = [(1,"a"); (2,"b"); (3,"c")] ;; run_test "zip [1;2;3][a;b;c]" test ;; print_string "lists.ml: ran to completion"