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

Bob Ippolito bob@REDACTED
Sun May 20 22:15:17 CEST 2007

On 5/20/07, Mike Berrow <mberrow1@REDACTED> wrote:
> 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?

You have to use an anonymous function if you're doing it directly in
the shell. The easiest way is to just create a module though. If
you're using Emacs it's just a couple keystrokes to save+compile,
which also does the purge+load in the shell.

Totally = fun (L) -> lists:sum([A * B || {A, B} <- L]) end.


More information about the erlang-questions mailing list