Skip to content

Instantly share code, notes, and snippets.

@muhammednagy
Created February 24, 2017 19:38
Show Gist options
  • Save muhammednagy/ea75ed93795b5112b82ab5439ccbbe69 to your computer and use it in GitHub Desktop.
Save muhammednagy/ea75ed93795b5112b82ab5439ccbbe69 to your computer and use it in GitHub Desktop.
Recursion in erlang (Fibonacci, factorial, n dimensions)
-module(recursion).
-export([fac/1,fib/1,cut/1]).
fac(0) ->
1;
fac(N) when N>0 ->
fac(N-1) * N;
fac(N) when N<0 ->
io:fwrite("Postieve number please", []).
fib(0) -> 0;
fib(1) -> 1;
fib(N) when N>0 ->
fib(N-1)+fib(N-2).
cut(0) -> 1;
cut(1) -> 2;
cut(N) when N>0 ->
cut(N-1)+N.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment