Skip to content

Instantly share code, notes, and snippets.

@lg0
Last active December 26, 2015 00:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lg0/7066135 to your computer and use it in GitHub Desktop.
Save lg0/7066135 to your computer and use it in GitHub Desktop.
Standard ML
(* Traditional (not tail-recursive) *)
fun fibonacci n =
case n of
0 => 0
| 1 => 1
| _ => fibonacci(n-1) + fibonacci(n-2)
(* tail-recursive *)
fun fbnc_tail n =
let fun aux (n, n_1, n_2) =
case n of
0 => n_2
| 1 => n_1
| _ => aux(n-1, n_1+n_2, n_1)
in aux(n, 1, 0)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment