Skip to content

Instantly share code, notes, and snippets.

@sbisbee
Created October 14, 2011 03:17
Show Gist options
  • Save sbisbee/1286171 to your computer and use it in GitHub Desktop.
Save sbisbee/1286171 to your computer and use it in GitHub Desktop.
Learning me some erlang by calculating Fibonacci.
-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