Fun, fun, Funs?

Torbjorn Tornkvist tobbe@REDACTED
Thu Sep 28 08:14:39 CEST 2000


A little example, from the Erlang shell:

8:07> erl
Erlang (BEAM) emulator version 5.0 [source]
Eshell V5.0  (abort with ^G)
1>  F=fun(X) -> X*2 end.
#Fun<erl_eval.17.63972567>

2> lists:map(F, [1,2,3,4]).
[2,4,6,8]

3> F2=fun(X,Acc) -> X+Acc end.
#Fun<erl_eval.15.83874>

4> lists:foldl(F2, 0, [1,2,3,4]).
10

5> F3=fun(X,Acc) -> {X*2,X+Acc} end.
#Fun<erl_eval.15.83874>

6> lists:mapfoldl(F3, 0, [1,2,3,4]).
{[2,4,6,8],10}

In a file you would have to write something like:

double_list(X) when list(X) ->
    F = fun(X) -> X*2 end,
    lists:map(F,X).


Cheers /Tobbe




More information about the erlang-questions mailing list