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

  by selfie on 23-Aug-2019 01:50 pm
We are known for our quality of support and customer service. This high quality support is delivered through our regional and branch offices across the country.
Broadcast consultants
  • Reply
  •  0 Like
  •  0 Dislike
  • Report