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().<br>
<br>-module(recurrent).<br>-export([lenGen/0, duplicateGen/0]).<br>-define(RC(P), (_This, P)).<br>-define(This, _This).<br>-define(Recur(F), begin _G = F, fun(_param) -> _G(_G,_param) end end).<br><br>lenGen() -><br>
?Recur(fun?RC([]) -> 0;<br> ?RC([_|T]) -> ?This?RC(T)+1<br> end).<br><br>duplicateGen() -><br> ?Recur(fun?RC([_, 0]) -> [];<br> ?RC([E, N]) -> [E|?This?RC([E, N-1])]<br>
end).<br><br><br><div class="gmail_quote">On Feb 7, 2008 12:49 PM, Hynek Vychodil <<a href="mailto:vychodil.hynek@gmail.com">vychodil.hynek@gmail.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;">
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).<div><div></div><div class="Wj3C7c"><br><br><div class="gmail_quote">
On Feb 6, 2008 11:03 PM, Zvi <<a href="mailto:exta7@walla.com" target="_blank">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><br><br>Christian S wrote:<br>><br>> On Feb 4, 2008 11:39 PM, Attila Babo <<a href="mailto:babo.online@gmail.com" target="_blank">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" target="_blank">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><br>_______________________________________________<br>erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">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></div></div><font color="#888888">-- <br>--Hynek (Pichi) Vychodil
</font></blockquote></div><br><br clear="all"><br>-- <br>--Hynek (Pichi) Vychodil