Source

kamlostuff / e21.ml

Full commit

exception TestFailed of string

let test test_func func_name input output () = 
  if test_func input <> output then
    raise (TestFailed (func_name ^ " doesn't work well!"))
  else ()    

let rec append lst appendix = match lst with
  | (h::t) -> (h::(append t appendix))
  | [] -> appendix

let test_append = 
  test (fun (i1, i2) -> append i1 i2) "append" 
    ([1;2], [42;23]) [1;2;42;23]

(* 'a list -> 'a list list *)
let suffixies_bad lst = 
  let rec worker l acc = match l with
    | (h::t) -> worker t (append acc [l])
    | [] -> append acc [[]]
  in worker lst []

let rec suffixes = function
  | [] -> [[]]
  | _ :: t as lst -> lst :: suffixes t

let test_suffixies =
  test suffixies "suffixies" [1;2] [[1;2]; [2]; []]

let generate_list count =
  let rec gen acc = function
    | 0 -> (0::acc)
    | x -> gen (x::acc) (x-1)
  in gen [] (count - 1)