[erlang-questions] Functional Programming

Gabriel Pack grpack@REDACTED
Wed Aug 29 21:01:52 CEST 2007


Yes, your function fac/1 is using recursion to calculate the product  
of the first N natural numbers.

Something to be worried about is if N is less than 1.

When you call fac(5), you get successive calls to fac(4), fac(3), and  
fac(2). All 4 of these calls go to the second clause of your function  
(i.e. fac(N)).

The final call is to fac(1) which is handled by the first clause of  
your function (i.e. fac(1)). This is the terminal case of your  
recursion.


Gabe


On Aug 29, 2007, at 9:58 AM, Lone Wolf wrote:

> Hi.
> Consider please this Erlang code:
>
> code:
>
> fac(1) ->
>   1;
> fac(N) ->
>   N * fac(N - 1).
>
> I don't understand whats happening when I call:
> fac(5)
> I'm not sure but it seems to me it is "Recursion", right?
> 5 * fac(4)
> 4 * fac(3)
> 3 * fac(2)
> etc..
> Thanks.
>
>
> Deep into that darkness peering, long I stood there, wondering,  
> fearing, Doubting, dreaming dreams no mortal ever dreamed before.
> E.A Poe
>
>
> Boardwalk for $500? In 2007? Ha!
> Play Monopoly Here and Now (it's updated for today's economy) at  
> Yahoo! Games.
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20070829/6b1b4593/attachment.htm>


More information about the erlang-questions mailing list