what happens if i apply a Fun on a empty list

Håkan Stenholm <>
Fri Aug 5 01:55:30 CEST 2005


MEENA SELVAM wrote:

>Res = apply(Fun, [])
>
>What will be in Res? will it have the return value of
>function;
>
>or something is wrong with the above statement?
>
>  
>
The above is ok and will contain the result of the call to "Fun()" call 
in Res, i.e. if the call is a call to a valid function things will look 
like below:

%% call math:pi()

1> apply(fun math:pi/0, []).
3.14159

%% call lists:append([1,2,3],[4,5,6])

2> apply(fun lists:append/2, [[1,2,3],[4,5,6]]).
[1,2,3,4,5,6]




If the call is to a non-existent function like in the example below ( 
lists:append() ), there will instead be a runtime execption.

3> apply(fun lists:append/2, []).              

=ERROR REPORT==== 5-Aug-2005::01:39:26 ===
Error in process <0.30.0> with exit value: 
{{badarity,{#Fun<lists.append.2>,[]}},[{erl_eval,do_apply,5},{shell,exprs,6},{shell,eval_loop,3}]}

** exited: {{badarity,{#Fun<lists.append.2>,[]}},
            [{erl_eval,do_apply,5},{shell,exprs,6},{shell,eval_loop,3}]} **
 



Documentation about apply and funs is available in the "erlang" module 
documentation 
(http://erlang.se/doc/doc-5.4.8/lib/kernel-2.10.9/doc/html/erlang.html)
and in the "Erlang Reference Manual" page chapter 6.17 
(http://erlang.se/doc/doc-5.4.8/doc/reference_manual/expressions.html#6.17). 

There are also more info about funs in Programming Examples chapter 2
(http://erlang.se/doc/doc-5.4.8/doc/programming_examples/funs.html#2)

>meena
>
>
>		
>__________________________________ 
>Do you Yahoo!? 
>Read only the mail you want - Yahoo! Mail SpamGuard. 
>http://promotions.yahoo.com/new_mail 
>
>  
>




More information about the erlang-questions mailing list