newbie question about fun expressions

Håkan Stenholm <>
Sat Sep 17 19:47:45 CEST 2005


Jose Balado wrote:

>Hi, I have a question:
>
>In "Erlang reference manual" 6.17 Fun Expressions is says that the following 
>fun expressions are also allowed: 
>
>fun Name/Arity
>fun Module:Name/Arity
>
> So I tried "fun Name/Arity" like this:
>
>46> lists:map(fun is_atom/1,[peter,1]).
>[true,false]
>47>  
>
>But "fun Module:Name/Arity" seems that does not work:
>
>47> lists:map(fun lists:reverse/1,[peter,1]).
>** 1: syntax error before: ':' **
>47> 
>
> What am I doing wrong?
>  
>

You should ensure that your using a recent version of Erlang/OTP as the 
"fun Module:Name/Arity" syntax is fairly recent. My code has been tested 
in Erlang/OTP R10B-6.

1) is_atom/1 in source code

test.erl
================

module(test).

-export([test/0]).


test() ->
    lists:map(fun is_atom/1,[peter,1]). %% 'is_atom/1' and 
'erlang:is_atom/1' both work

================

erlang shell
================
2> test:test().
[true,false]


This works as functions in the module 'erlang' don't require to be 
called as 'erlang:function(...)', the erlang shell which sometimes works 
slightly different from regular code, doesn't appear to be able to 
figure out where to look for is_atom/1 if the module isn't supplied, as 
can be seen below:


2) call in erlang shell, using Function/Arity syntax

2> lists:map(fun is_atom/1,[peter,1]).

=ERROR REPORT==== 17-Sep-2005::19:13:03 ===
Error in process <0.30.0> with exit value: 
{undef,[{erl_eval,is_atom,1},{erl_eval,expr,3}]}
** exited: {undef,[{erl_eval,is_atom,1},{erl_eval,expr,3}]} **

the function is_atom/1 is not found in erl_eval (the "module" of the 
shell), i.e. it's 'undefined', when erl_eval tries to process 
'lists:map(fun is_atom/1,[peter,1]).'.

3) call in erlang shell, using Module:Function/Arity syntax works as 
expected

3> lists:map(fun erlang:is_atom/1,[peter,1]).
[true,false]



>Thanks.
>
>Best regards,
>Jose
>
>
>
>
>  
>




More information about the erlang-questions mailing list