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

andrew cooke <>
Sun May 20 22:03:41 CEST 2007


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


> I can load function in from files using
> c(modname).
>
> But when I try the same thing in the shell I get an error.
> eg.
>
> 10> Prs = [{2,7},{6,2},{4,3},{2,5}].
> [{2,7},{6,2},{4,3},{2,5}]
> 11> totally(L) -> lists:sum([A * B || {A, B} <- L]).
> ** 1: syntax error before: '->' **
>
> Coming from ruby, I a used experimenting with code fragments in the shell
> (very useful).
> Is there a different way to do it?   or is it just not possible?
>
> Thanks,
> -- Mike Berrow
>
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
>





More information about the erlang-questions mailing list