XMPP JavaScript Library

Factorial program in erlang using fold, recursion and guards

Erlang, Sachin Puri, 2016-11-19 00:47:12

Using Recursion

This is an example program to show how can we make use of recursion in erlang to write a factorial program.


fact(0)-> 1;
fact(N)-> N * fact(N-1).

Using Recursion (With Guard)

This is an example program to show how can we make use of guard with recursion in erlang to write a factorial program.


fact(N) when N == 0 -> 1;
fact(N) when N > 0 -> N * fact(N-1).

Using Fold

This is an example program to show how can we make use of fold in erlang to write a factorial program.


lists:foldl(fun(A,B)-> A*B end , 1, [1,2,3,4,5,6,7,8,9,10]).

 

Add Your Comment
   
    Yes! I want to receive all comments by email

No Comments Posted Yet. Be the first one to post comment