[erlang-questions] Auto generated functions
Thu Feb 7 13:30:14 CET 2008
It is possible to add this() functionality by macro expansion without
various number of parameters. But varios number of parameters can be done by
some parse transformation. In this case I think this() is really only
syntactic sugar. Just add letrec syntax element and you can forgot this().
-define(RC(P), (_This, P)).
-define(Recur(F), begin _G = F, fun(_param) -> _G(_G,_param) end end).
?Recur(fun?RC() -> 0;
?RC([_|T]) -> ?This?RC(T)+1
?Recur(fun?RC([_, 0]) -> ;
?RC([E, N]) -> [E|?This?RC([E, N-1])]
On Feb 7, 2008 12:49 PM, Hynek Vychodil <vychodil.hynek@REDACTED> wrote:
> Why if you can just use "half" of Y combinator:
> 3> LenGen = fun(_, ) -> 0; (This, [_|T]) -> This(This, T)+1 end,
> 3> Len = fun(L) -> LenGen(LenGen, L) end.
> 4> Len([a,b,c]).
> 5> Len().
> 6> Len(lists:seq(1,100)).
> 7> Len2 = begin G = fun(_, ) -> 0; (This, [_|T]) -> This(This, T)+1 end,
> fun(L) -> G(G, L) end end.
> 8> Len2().
> 9> Len2([a,b,c]).
> 10> Len2(lists:seq(1,100)).
> this() is only syntactic sugar and object oriented approach (not
> On Feb 6, 2008 11:03 PM, Zvi <exta7@REDACTED> wrote:
> > Y-combinator is cool, but I'm just currious why Erlang doesn't have some
> > mechanism allowing fun to reference to itself (doesn't mutter if it's
> > named
> > or annonymous fun), same way as every process can get it's own PID using
> > self(), i.e. (here I using this() to reference to function from inside):
> > Len = fun() -> 0;
> > ([_|T]) -> this()(T)+1 end.
> > Is there something in BEAM instruction set, that preventing from
> > implementing this?
> > thanks,
> > Zvi
> > Christian S wrote:
> > >
> > > On Feb 4, 2008 11:39 PM, Attila Babo <babo.online@REDACTED> wrote:
> > >> I wrote a simple Y combinator to deal with tail-recursive anonymous
> > >> functions. There are several showcase implementations for Erlang
> > >> already, but for practical purposes you need to deal with arity. Here
> > >> is my code with examples, I'm looking for a way to avoid this
> > >> boilerplate code. For practical reasons it's OK to generate it up to
> > a
> > >> point by hand or use a single tuple as a parameter, but I'm looking
> > >> for "nicer" solution. Any suggestions?
> > >
> > > That's the way its done. Only improvement would be to use the
> > > erlang:is_funciton/2 guard. But that is not likely what you were
> > looking
> > > for.
> > > _______________________________________________
> > > erlang-questions mailing list
> > > erlang-questions@REDACTED
> > > http://www.erlang.org/mailman/listinfo/erlang-questions
> > >
> > >
> > --
> > View this message in context:
> > http://www.nabble.com/Auto-generated-functions-tp15279499p15312902.html
> > Sent from the Erlang Questions mailing list archive at Nabble.com.
> > _______________________________________________
> > erlang-questions mailing list
> > erlang-questions@REDACTED
> > http://www.erlang.org/mailman/listinfo/erlang-questions
> --Hynek (Pichi) Vychodil
--Hynek (Pichi) Vychodil
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the erlang-questions