Why if you can just use "half" of Y combinator:<br><br>3> LenGen = fun(_, []) -> 0; (This, [_|T]) -> This(This, T)+1 end,<br>3> Len = fun(L) -> LenGen(LenGen, L) end.<br>#Fun<erl_eval.6.49591080><br>
4> Len([a,b,c]).<br>3<br>5> Len([]).<br>0<br>6> Len(lists:seq(1,100)).<br>100<br>7> Len2 = begin G = fun(_, []) -> 0; (This, [_|T]) -> This(This, T)+1 end, fun(L) -> G(G, L) end end.<br>#Fun<erl_eval.6.49591080><br>
8> Len2([]).<br>0<br>9> Len2([a,b,c]).<br>3<br>10> Len2(lists:seq(1,100)).<br>100<br><br>this() is only syntactic sugar and object oriented approach (not functional).<br><br><div class="gmail_quote">On Feb 6, 2008 11:03 PM, Zvi <<a href="mailto:exta7@walla.com">exta7@walla.com</a>> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br>Y-combinator is cool, but I'm just currious why Erlang doesn't have some<br>mechanism allowing fun to reference to itself (doesn't mutter if it's named<br>
or annonymous fun), same way as every process can get it's own PID using<br>self(), i.e. (here I using this() to reference to function from inside):<br><br>Len = fun([]) -> 0;<br>             ([_|T]) -> this()(T)+1 end.<br>
<br>Is there something in BEAM instruction set, that preventing from<br>implementing this?<br><br>thanks,<br>Zvi<br><div><div></div><div class="Wj3C7c"><br><br>Christian S wrote:<br>><br>> On Feb 4, 2008 11:39 PM, Attila Babo <<a href="mailto:babo.online@gmail.com">babo.online@gmail.com</a>> wrote:<br>
>> I wrote a simple Y combinator to deal with tail-recursive anonymous<br>>> functions. There are several showcase implementations for Erlang<br>>> already, but for practical purposes you need to deal with arity. Here<br>
>> is my code with examples, I'm looking for a way to avoid this<br>>> boilerplate code. For practical reasons it's OK to generate it up to a<br>>> point by hand or use a single tuple as a parameter, but I'm looking<br>
>> for "nicer" solution. Any suggestions?<br>><br>> That's the way its done. Only improvement would be to use the<br>> erlang:is_funciton/2 guard. But that is not likely what you were looking<br>
> for.<br>> _______________________________________________<br>> erlang-questions mailing list<br>> <a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>> <a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>
><br>><br><br></div></div><font color="#888888">--<br>View this message in context: <a href="http://www.nabble.com/Auto-generated-functions-tp15279499p15312902.html" target="_blank">http://www.nabble.com/Auto-generated-functions-tp15279499p15312902.html</a><br>
Sent from the Erlang Questions mailing list archive at <a href="http://Nabble.com" target="_blank">Nabble.com</a>.<br></font><div><div></div><div class="Wj3C7c"><br>_______________________________________________<br>erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br><a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>--Hynek (Pichi) Vychodil