<br><br><div class="gmail_quote">---------- Forwarded message ----------<br>From: <b class="gmail_sendername">黃耀賢 (Yau-Hsien Huang)</b> <span dir="ltr"><<a href="mailto:g9414002.pccu.edu.tw@gmail.com">g9414002.pccu.edu.tw@gmail.com</a>></span><br>
Date: 2012/3/30<br>Subject: Re: [erlang-questions] How do funs work ?<br>To: Yoshihiro Tanaka <<a href="mailto:hirotnkg@gmail.com">hirotnkg@gmail.com</a>><br><br><br><div class="gmail_quote"><div class="im">2012/3/30 Yoshihiro Tanaka <span dir="ltr"><<a href="mailto:hirotnkg@gmail.com" target="_blank">hirotnkg@gmail.com</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi Yau-Hsien,<br>
<br>
It seems like not. For example, below example indicates F is not<br>
evaluated when constructing F2.<br>
<br>
1> F = fun() -> blah end.<br>
#Fun<erl_eval.20.21881191><br>
2> F2 = fun() -> 1 + 2 + F() end.<br></blockquote><div> </div></div><div>No. Here you type F() which means F is evaluated when F2 is evaluating.</div><div>I said the word "evaluated" but "reduced."</div>
<div><br></div><div>If you expand the symbol 'F' to the function, in syntax, it must be</div><div> F2 = fun() -> 1 + 2 + (fun -> 0 end)() end.</div><div>but</div><div> F2 = fun() -> 1 + 2 + (fun() -> 0 end) end.</div>
<div>Later one is depicted in your original letter, then I point the difference</div><div>on parenthesis after. Maybe it's ill-wording, that it ought to be function application</div><div>for symbol 'F()' and function-taking for symbol 'F'.</div>
<div class="im">
<div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">#Fun<erl_eval.20.21881191><br>
3> F2().<br>
** exception error: bad argument in an arithmetic expression<br>
in operator +/2<br>
called as 3 + blah<br>
<br>
I also checked core file of sample code, using 'to_core' option, and the code:<br>
18 fun() -> 9 + 10 +<br>
19 fun() -> 0 end()<br>
20 end()<br></blockquote><div> </div></div><div>The part of the core file seems as what I mentioned. :)</div><div><div class="h5"><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
is translated to:<br>
%% Line 18<br>
( fun () -><br>
let <_cor0> =<br>
%% Line 19<br>
( fun () -><br>
0<br>
-| [{'id',{2,100470014,'-make_fun2/0-fun-0-'}}] )<br>
in let <_cor1> =<br>
%% Line 19<br>
apply _cor0<br>
()<br>
in call 'erlang':'+'<br>
(19, _cor1)<br>
-| [{'id',{3,119737161,'-make_fun2/0-fun-1-'}}] )<br>
<br>
It defines <_cor0>, as fun() -> 0 end, then doing 'apply _cor0'. So<br>
It's calling fun _cor0.<br>
<span><font color="#888888"><br>
<br>
Yoshihiro<br>
</font></span><div><div><br>
<br>
<br>
><br>
>><br>
>> Also, is there any difference between funs that are defined at runtime<br>
>> and funs that are defined at compile time in terms of how they are<br>
>> executed ?<br>
>><br>
>> Thank you<br>
>> Yoshihiro<br>
>><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://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
><br>
><br>
><br>
><br>
> --<br>
><br>
> Best Regards.<br>
><br>
> --- Y-H. H.<br>
><br>
><br>
</div></div></blockquote></div></div></div><div class="HOEnZb"><div class="h5"><br><br clear="all"><div><br></div>-- <br><div><br></div>Best Regards.<div><br></div><div>--- Y-H. H.</div><div><br></div><br>
</div></div></div><br><br clear="all"><div><br></div>-- <br><div><br></div>Best Regards.<div><br></div><div>--- Y-H. H.</div><div><br></div><br>