# [erlang-questions] How do funs work ?

Yoshihiro Tanaka <>
Wed Mar 28 19:55:52 CEST 2012

```Yes, you are right. But my point is, in this fun, each part of 'fun()
-> something end()' will be executed during F2 is created ?
or that part will be executed when I call F2 like F2() ? I'm not sure
how it works.

I'm also curious about when funs are created at runtime, is there any
difference from hard coded funs? It seems like that I can not extract
abstract code from funs that are created at runtime using
'erlang:fun_info/2'. I'd appreciate if someone could give me a
pointer.

Thank you
Yoshihiro

On Wed, Mar 28, 2012 at 4:01 AM, Hynek Vychodil <> wrote:
> I think it should be
>
> F2 = fun() -> 9 + 10 +
>       fun() -> 7 + 8 +
>         fun() -> 5 + 6 +
>           fun() -> 3 + 4 +
>             fun() -> 1 + 2 +
>               fun() -> 0 end()
>             end()
>           end()
>         end()
>       end()
>     end.
>
>
> On Wed, Mar 28, 2012 at 10:51 AM, Yoshihiro Tanaka <> wrote:
>> Hello,
>>
>> I have a function which returns fun as below:
>>
>> make_fun(_, [], F) -> F;
>> make_fun(Operator, [Operand1, Operand2|Operands], F) ->
>>  F2 = case Operator of
>>         plus  -> fun() -> Operand1 + Operand2 + F() end;
>>         minus -> fun() -> Operand1 - Operand2 + F() end
>>       end,
>>  make_fun(Operator, Operands, F2).
>>
>> When I call it as:
>> F1 = make_fun(plus, lists:seq(1,10), fun() -> 0 end).
>>
>> Is F1 same as F2 below ?
>> F2 = fun() -> 1 + 2 +
>>       fun() -> 3 + 4 +
>>         fun() -> 5 + 6 +
>>           fun() -> 7 + 8 +
>>             fun() -> 9 + 10 +
>>               fun() -> 0 end()
>>             end()
>>           end()
>>         end()
>>       end()
>>     end.
>>
>>
>> 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
>
>
>
> --
> Hynek Vychodil
> BI consultant
>
> GoodData
> náměstí 28. října 1104/17, 602 00, Brno - Černá Pole
> Office:   +420 530 50 7704
> E-mail:
> Web:     www.gooddata.com

```