Skip to content

Instantly share code, notes, and snippets.

@susisu
Last active June 8, 2017 13:24
Show Gist options
  • Select an option

  • Save susisu/9726ef23713d562585a1a0187873eaef to your computer and use it in GitHub Desktop.

Select an option

Save susisu/9726ef23713d562585a1a0187873eaef to your computer and use it in GitHub Desktop.
let rec length1 = function
| [] -> 0
| _ :: tl -> 1 + length1 tl
let length2 list =
let rec _length2 n = function
| [] -> n
| _ :: tl -> _length2 (n + 1) tl
in
_length2 0 list
let length3 list =
let rec _length3 list cont = match list with
| [] -> cont 0
| _ :: tl -> _length3 tl (fun n -> cont (n + 1))
in
_length3 list (fun n -> n)
let repeat elem num =
let rec _make_list list n = if n > 0
then _make_list (elem :: list) (n - 1)
else list
in
_make_list [] num
let test name func =
let list = repeat 0 100000 in
try
let len = func list in
print_endline (name ^ " " ^ string_of_int len)
with
_ -> print_endline (name ^ " :(")
let () = test "length1" length1
let () = test "length2" length2
let () = test "length3" length3
length1 :(
length2 100000
length3 :(
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment