[erlang-questions] re cursive fun()

Hynek Vychodil vychodil.hynek@REDACTED
Tue Oct 7 17:47:08 CEST 2008


Yes of course, it can be done. There is little difference when closure is
made.

this second example can be rewritten (and simplified) in same manner:

UpToGen = fun(Max) ->
              Outer = fun(_OSelf, Max) -> [];
                                (OSelf, Step) ->
                                     Inner = fun(_ISelf, N) when N>Max ->
[];
                                                    (ISelf, N) -> [N |
ISelf(ISelf, N+Step) ]
                                     end,
                               [Inner(Inner, 0) | OSelf(OSelf, Step+1)]
              end,
              Outer(Outer, 1)
  end.

On Tue, Oct 7, 2008 at 4:29 PM, David Mercer <dmercer@REDACTED> wrote:

>  On Tuesday, October 07, 2008, Hynek Vychodil wrote:
>
>
>
> > Is there any problem write this think this way?
>
> . . .
>
> > For Example factorial:
>
> >
>
> > Fact = begin F = fun(_, 0) -> 1; (Self, N) -> Self(Self, N-1)*N end,
> fun(N) -> F(F, N) end end.
>
>
>
> My only objection is that it pollutes the shell variable space with the
> extra function F.  How about:
>
>
>
> Fact = fun(N) -> F = fun(_, 0) -> 1; (Self, N) -> Self(Self, N-1)*N end,
> F(F, N) end.
>
>
>
> Cheers,
>
>
>
> David
>



-- 
--Hynek (Pichi) Vychodil
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20081007/667cb54e/attachment.htm>


More information about the erlang-questions mailing list