[erlang-questions] Functional Programming
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
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
On Aug 29, 2007, at 9:58 AM, Lone Wolf wrote:
> Consider please this Erlang code:
> fac(1) ->
> fac(N) ->
> N * fac(N - 1).
> I don't understand whats happening when I call:
> I'm not sure but it seems to me it is "Recursion", right?
> 5 * fac(4)
> 4 * fac(3)
> 3 * fac(2)
> 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
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the erlang-questions