[erlang-questions] Passing functions as arguments requires export?

Kenny Stone <>
Wed Nov 3 18:35:31 CET 2010


'incr' will be called from the lists module, not the test module, so it
cannot access it without being exported.

This is one way to do what you want:

res() -> lists:map(fun incr/1, [0,1,2]).

Kenny

On Wed, Nov 3, 2010 at 12:24 PM, Michal D. <>wrote:

> Hi All,
>
> I'm having some problems working with functions in Erlang.  Specifically,
> passing functions as arguments. Given the following module:
>
> -module(test).
> -export([res/0]).
>
> res() -> lists:map({test,incr}, [0,1,2]).
>
> incr(X) -> X + 1.
>
> I stumbled upon the syntax {test,incr} to pass a (not anonymous) function.
> When I go to run this code in the emulator, I get an error that doesn't get
> resolved unless I add "incr/1" to the export list.  However, there's no
> reason to export the function because I don't ever want to call it
> directly.  What am I missing?
>
> > c(test).
> > test:res().
> ** exception error: undefined function test:incr/1
>     in function  lists:map/2
> >
>
> Cheers,
>
> Michal
>


More information about the erlang-questions mailing list