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 james thomas on 13-Feb-2019 12:11 am
The points you make in this content are very clear, interesting and informative. I agree with several points here. Please continue these articles as they are very engaging. Great job! Shoreline Amphitheatre tickets
  • Reply
  •  0 Like
  •  0 Dislike
  • Report