[erlang-questions] Why do we need modules at all?

Vlad Dumitrescu <>
Tue May 24 11:00:23 CEST 2011


Hi,

On Tue, May 24, 2011 at 10:06, Joe Armstrong <> wrote:

> Do we need module's at all? Erlang programs are composed of lots of small
> functions, the only place where modules seem useful is to hide a letrec.
>
> The classic example is fibonacci. We want to expose fib/1 but hide the
> helper function fib/3. Using modules we say
>
> -module(math).
> -export([fib/1]).
>
> fib(N) ->
>     fib(N, 1, 0).
>
> fib(N, A, B) when N < 2 -> A;
> fib(N, A, B) -> fib(N-1, A+B, A).
>
> The downside is we have had to *invent* one module name math - whose *only*
> purpose is to hide the definition of fib/3 which we don't want to be made
> callable.
>
> If we put a second function into the module math, then this second function
> could call fib/3 which breaks the encapsulation of fib/3.
>
> We could say:
>
> let fib = fun(N) -> fib(N, 1, 0) end
> in
>    fib(N, A, B) when N < 2 -> A;
>    fib(N, A, B) -> fib(N-1, A+B, A).
> end.


One reason where letrec might not be a good enough replacement for a helper
function that is private to a module, is if this helper function is called
from many places. I wouldn't want to have to repeat its definition every
time I need it...

I also feel like Mazen that unless there is a big shift in the way we think
about the programs, the problem will still be present at the prefix level.

If there is to be a big shift in the paradigms, then the question is whether
the result will still be Erlang...

regards,
Vlad


>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20110524/ac891c4d/attachment.html>


More information about the erlang-questions mailing list