[erlang-questions] How do you define a function using the shell?

Mike Berrow mberrow1@REDACTED
Sun May 20 22:17:10 CEST 2007


Thanks, that helps a lot.
I tried it this way.

14> Totally = fun(L) -> lists:sum([A * B || {A, B} <- L]) end.
#Fun<erl_eval.6.72228031>
15> Totally(Prs).
48
16> Prs.
[{2,7},{6,2},{4,3},{2,5}]

I guess I will have to learn not to expect the environment
of the shell and compiled-file to be as interchangable as that.

-- Mike Berrow

> i don't have a definitive answer, but a quick sunday afternoon fix that
> may help is to note that you can (i) bind to variables and (ii) the
> anonymous function definition (fun...end) supports the usual pattern
> matching clauses.
> 
> so:
> 11> F = fun(1) -> "one";
> 11> (N) -> "lots"
> 11> end.
> #Fun<erl_eval.6.72228031>
> 12> F(3).
> "lots"
> 
> andrew




More information about the erlang-questions mailing list