[erlang-questions] How do funs work ?

Yoshihiro Tanaka hirotnkg@REDACTED
Fri Mar 30 07:05:32 CEST 2012


2012/3/29 黃耀賢 (Yau-Hsien Huang) <g9414002.pccu.edu.tw@REDACTED>:
> 2012/3/30 Yoshihiro Tanaka <hirotnkg@REDACTED>
>>
>> Hi Yau-Hsien,
>>
>> It seems like not. For example, below example indicates F is not
>> evaluated when constructing F2.
>>
>> 1> F = fun() -> blah end.
>> #Fun<erl_eval.20.21881191>
>> 2> F2 = fun() -> 1 + 2 + F() end.

This example was not helpful to check if F is executed when
constructing F2. I think I should have written as:

2> EvalCheckFun = fun() -> 1 + 2 + fun() -> io:format("blah~n"), blah end() end.
#Fun<erl_eval.20.21881191>
3> EvalCheckFun().
blah
** exception error: bad argument in an arithmetic expression
     in operator  +/2
        called as 3 + blah

>
>
> No. Here you type F() which means F is evaluated when F2 is evaluating.
> I said the word "evaluated" but "reduced."
>
> If you expand the symbol 'F' to the function, in syntax, it must be
>     F2 = fun() -> 1 + 2 + (fun -> 0 end)() end.
> but
>     F2 = fun() -> 1 + 2 + (fun() -> 0 end) end.
> Later one is depicted in your original letter, then I point the difference

Maybe I'm not understanding something, but I can not find it. Could
you point that line in my original post ?
My current understanding is when I write like below, the part '(fun ->
0 end)()' is not evaluated/executed yet when F2 is bound, and it will
be evaluated when F2 is evaluated as F2().

F2 = fun() -> 1 + 2 + (fun -> 0 end)() end.

Is this understanding different from yours ?



> on parenthesis after. Maybe it's ill-wording, that it ought to be function
> application
> for symbol 'F()' and function-taking for symbol 'F'.
>
>
>> #Fun<erl_eval.20.21881191>
>> 3> F2().
>> ** exception error: bad argument in an arithmetic expression
>>     in operator  +/2
>>        called as 3 + blah
>>
>> I also checked core file of sample code, using 'to_core' option, and the
>> code:
>> 18           fun() -> 9 + 10 +
>> 19             fun() -> 0 end()
>> 20           end()
>
>
> The part of the core file seems as what I mentioned. :)
>
>>
>> is translated to:
>> %% Line 18
>> ( fun () ->
>>      let <_cor0> =
>>    %% Line 19
>>    ( fun () ->
>>    0
>>      -| [{'id',{2,100470014,'-make_fun2/0-fun-0-'}}] )
>>      in  let <_cor1> =
>>        %% Line 19
>>        apply _cor0
>>      ()
>>    in  call 'erlang':'+'
>>      (19, _cor1)
>>  -| [{'id',{3,119737161,'-make_fun2/0-fun-1-'}}] )
>>
>> It defines <_cor0>, as fun() -> 0 end, then doing 'apply _cor0'. So
>> It's calling fun _cor0.
>>
>>
>> Yoshihiro
>>
>>
>>
>> >
>> >>
>> >> Also, is there any difference between funs that are defined at runtime
>> >> and funs that are defined at compile time in terms of how they are
>> >> executed ?
>> >>
>> >> Thank you
>> >> Yoshihiro
>> >>
>> >> _______________________________________________
>> >> erlang-questions mailing list
>> >> erlang-questions@REDACTED
>> >> http://erlang.org/mailman/listinfo/erlang-questions
>> >
>> >
>> >
>> >
>> > --
>> >
>> > Best Regards.
>> >
>> > --- Y-H. H.
>> >
>> >
>
>
>
>
> --
>
> Best Regards.
>
> --- Y-H. H.
>
>



More information about the erlang-questions mailing list