Last active
December 26, 2015 00:39
-
-
Save lg0/7066135 to your computer and use it in GitHub Desktop.
Standard ML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* 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