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

Joe Armstrong <>
Mon May 21 09:54:34 CEST 2007


It's not possible - but it *should be possible*

When I was writing the Erlang book Dave Thomas asked be precisely this question
"why can't you just type the module code into the shell"

I gave my knee-jerk reaction - "because ..."

He countered, "but it must be easy - all you have to do is pipe all the shell
output into a file and compile it ...."

He (and Mike) are right (of course). Module code and "what you can
write in a shell"
are different and it's been a pain in the arse explaining in the book,
in the errata to the
book and in this group *why* they are different.

Variable in the shell look pretty much like bound variables in
parameterized modules.

It wouldn't be that difficult to let the shell and module code be
interchangable.

There are some problems with this.

Supppose this was legal in the shell AND in a module

> -module(a).
> X = foo:bar(...)
> foo(A, B) ->
>     A+B+X.
> -end(module)

Then foo would have to be available when we compile bar and we would run into
version nastyness.

A suitable compromise might be "everything you can type in a module
can be typed into
the shell" but NOT the other way around.

On the other hand, if we solved the version nastyness problem then doing
shell like things in module would be great for writing efficient code AND we
could throw away the parse transform stuff, doing it with regular functions.

/Joe








On 5/20/07, Mike Berrow <> 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?
>
> Thanks,
> -- Mike Berrow
>
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list