[erlang-questions] Fwd: How do funs work ?

黃耀賢 (Yau-Hsien Huang) <>
Fri Mar 30 02:15:55 CEST 2012


---------- Forwarded message ----------
From: 黃耀賢 (Yau-Hsien Huang) <>
Date: 2012/3/30
Subject: Re: [erlang-questions] How do funs work ?
To: Yoshihiro Tanaka <>


2012/3/30 Yoshihiro Tanaka <>

> 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.
>

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
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
> >> 
> >> http://erlang.org/mailman/listinfo/erlang-questions
> >
> >
> >
> >
> > --
> >
> > Best Regards.
> >
> > --- Y-H. H.
> >
> >
>



-- 

Best Regards.

--- Y-H. H.





-- 

Best Regards.

--- Y-H. H.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120330/5e900bc3/attachment.html>


More information about the erlang-questions mailing list