Newbie questions

Javier París Fernández paris@REDACTED
Sat Mar 25 11:27:57 CET 2006


El Sábado, 25 de Marzo de 2006 08:55, Nick Linker escribió:

Hi,

> Good day.
> 3. Let me define the following function:
>     fib(0) -> 0;
>     fib(1) -> 1;
>     fib(N) -> fib(N-1) + fib(N-2).
> I have failed to convert this function _without_ put/get to be able to
> compute even fib(100) within reasonable period of time (I guess I did it
> wrong so that tail recursion was not here). Is there a way to compute
> fib(N), N>=100 without side effects?

fibaux(_N2,N1,N,N) -> N1;
fibaux(N2,N1,C,N) ->
        fibaux(N1,N1+N2,C+1,N).

fib(0) -> 0;
fib(N) ->
        fibaux(0,1,0,N).

However, as Kostis said, this has more to do with having two recursive calls 
each time than with it being tail recursion or not. If you try to see how it 
evaluated, the number of recursive calls expands exponentially.

Regards.

	



More information about the erlang-questions mailing list