Skip to content

Instantly share code, notes, and snippets.

@tambow44
Created May 13, 2020 06:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tambow44/90fd6b94b46d55e64a3cdb1383102508 to your computer and use it in GitHub Desktop.
Save tambow44/90fd6b94b46d55e64a3cdb1383102508 to your computer and use it in GitHub Desktop.
2nd week of Erlang on FutureLearn (2.3)
-module fourth.
-export[fac/1,fib/1,pieces/1].
fac(0) ->
1;
fac(N) when (N > 0) ->
fac(N - 1) * N
.
fib(0) ->
0;
fib(1) ->
1;
fib(N) when (N > 0) ->
fib(N - 2) + fib(N - 1)
.
% Step through of 'fib(4)':
% __fib(4)
% = fib(2) + fib(3)
% = (fib(0) + fib(1)) + (fib(1) + fib(2))
% = (0 + 1) + (1 + (fib(0) + fib(1)))
% = 1 + 1 + (0 + 1)
% = 1 + 1 + 1
% = 3
pieces(0) ->
1;
pieces(N) when (N > 0) ->
N + pieces(N-1)
.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment