[erlang-questions] Recursion in the shell=impossible?
Matthew Reilly
matthew.reilly@REDACTED
Tue Jun 17 05:38:56 CEST 2008
It's possible, but ugly. You need to create an anonymous function that
has an additional argument of type Fun.
You then call this function, passing the function to itself.
Examples speak louder than words:
erl> Fact = fun(N) ->
F1 = fun
(0,_F) -> 1;
(M,F) -> M * F(M-1,F)
end,
F1(N,F1)
end.
erl> Fact(5).
120
Circular Function wrote:
> 6> Fac = fun(X) -> N * Fac(N-1).
> * 1: syntax error before: '.'
> 6> Fac = fun(X) -> N * Fac(N - 1).
> * 1: syntax error before: '.'
> 6> Fac = fun(X) -> N * fac(N - 1).
> * 1: syntax error before: '.'
> 6> fac = fun(X) -> N * fac(N - 1).
> * 1: syntax error before: '.'
>
> is it not possible to write recursive functions in the shell?
> i have to do that ina module?
> can i write functiosn any other way than making an anonymous one and
> assigning it to a variable?
> 6> Sq = fun(X) -> X*X end.
> #Fun<erl_eval..6.13229925>
> 7> Sq(12).
> 144
> 8>
>
>
> ------------------------------------------------------------------------
> Låna pengar utan säkerhet.
> Sök och jämför hos Yahoo! Shopping.
> <http://shopping.yahoo.se/c-100390123-lan-utan-sakerhet.html?partnerId=96915014>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
More information about the erlang-questions
mailing list