Created
October 14, 2011 03:17
-
-
Save sbisbee/1286171 to your computer and use it in GitHub Desktop.
Learning me some erlang by calculating Fibonacci.
This file contains hidden or 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
-module(fibo). | |
-export([calc/1, run/1]). | |
% Entry method to calculate fibo | |
calc(N) when N > 0 -> | |
calc(N, 1, 1, []); | |
calc(_) -> | |
'Can only calculate F(N) where N > 0'. | |
% The actual fibonacci calculation. | |
calc(1, A, _B, List) -> | |
List ++ [ A ]; | |
calc(N, A, B, List) -> | |
calc(N - 1, B, A + B, List ++ [ A ]). | |
% A runner that creates a 2D list of fibo lists from F(1) to F(N). | |
run(N) when N > 0 -> | |
run(1, N + 1, []); | |
run(_) -> | |
'Must specify a positive number'. | |
run(Iter, Max, List) when Iter < Max -> | |
run(Iter + 1, Max, List ++ [ calc(Iter) ]); | |
run(_, _, List) -> | |
List. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment