doubt on fun

Corrado Santoro csanto@REDACTED
Sun Jun 19 10:08:19 CEST 2005


MEENA SELVAM wrote:
> My doubt is what happens, when we put the fun as a
> parameter to a function. Since the fun is in the body,
> will the statements within the fun end block be
> executed just like any other expression?   
No, the fun/end block is executed when the function is evaluated.

I mean:

F = fun(X) -> X + 1 end.

This statement assigns to "F" the code of the fun. It is a simple 
assignment, not a "fun" execution.

When you write:

F(100)

you'll really execute the fun you declared above and get the result, 
"101" in this case.

When you pass a fun as a function parameter, a similar thing happens. 
The fun is assigned to an internal variable of the function and then it 
performs invocation, when needed.

> Similarly, what happens when fun... end is used as
> part of map as follows?
> 
> map(fun(X)->
>             {Y,Z}
>     end,
>     somelist)
> 
> In comparison with map(Func, List) applying Func to
> each element in the list, what does the above map with
> func .... end applies to the list?
It applies the fun to each element of the list as well. However, in your 
example, Z has to be bound to a value.

--Corrado

-- 
======================================================
Eng. Corrado Santoro, Ph.D.

University of Catania - Engineering Faculty
Department of Computer Science and
Telecommunications Engineering
Viale A. Doria, 6 - 95125 CATANIA (ITALY)

Tel: +39 095 7382144 Int. (5) 8035
      +39 095 7382380
      +39 095 7382365
      +39 095 7382364

VoIP: sip:8035@REDACTED

Fax: +39 095 7382397

EMail: csanto@REDACTED
Personal Home Page:
             http://www.diit.unict.it/users/csanto

NUXI Home Page:
             http://nuxi.iit.unict.it
======================================================




More information about the erlang-questions mailing list