Skip to content

Instantly share code, notes, and snippets.

@Davidslv
Created February 5, 2014 20:11
Show Gist options
  • Save Davidslv/8832054 to your computer and use it in GitHub Desktop.
Save Davidslv/8832054 to your computer and use it in GitHub Desktop.
Factorial function in erlang
-module(fact).
-export([factorial/1]).
%% The solution is tail recursive
factorial(N) ->
factorial(1, N, 1).
factorial(Current, N, Result) when Current =< N ->
NewResult = Result * Current,
factorial(Current+1, N, NewResult);
factorial(_, _, Result) ->
Result.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment