forget

Chris Pressey <>
Tue May 28 19:06:11 CEST 2002


On Tue, 28 May 2002 09:54:11 +0200 (CEST)
Joe Armstrong <> wrote:

> > 
> > I don't know if I misunderstand what you mean but one idea I have had
> > is to use something similar to Prolog DCG (definite clause grammar)
> > syntax so that the code can be written hiding the extra parameter
> > passed around to every call. For example something like
> > 
> >   foo() -->
> >          bar(),                   % Call to other --> functions that
> >          return X = baz(),               % implicitly return
> >          {AnyValue,HiddenVar} Val = v(),               % Special call
> >          v() returns hidden parameter
> >                                   % at the point of the call.
> >          c(Value = zab(Val,X)),   % Not a --> call, normal function
> >          call Value.
> > 
> > could be expanded by the preprocessor to
> > 
> >   foo(S0) ->
> >          {_,S1} = bar(S0),
> >          {X,S2} = baz(S1),
> >          Val = S2,            % or a call {Val,S3} = v(S2) if not
> >          special Value = zab(Val,X),
> >          {Value,S2}.          % or {Value,S3} if c/0 not special 
> > 
> >   v(S) -> {S,S}.
> > 
> > But I don't know how much cluttering is removed, if any, for real
> > programs. Works nice for Prolog but for Erlang it may not be as
> > useful,
> 
>   Umm - and then we can say "Erlang has got Monads!" 

And then we'll have to stop saying "Erlang is a single assignment
language" (since, technically, we're making multiple assignments to an
'invisible' variable.)

If we shall go this route, I suggest taking some unused special symbol and
calling it the 'default variable', which is always just an 'alias' for the
result of the previous evaluation.  This could be handy in the shell as
well, as a sort of accumulator for an adding machine:

1> 15.
15
2> _+20.
35
3> _+7.
42

Except, obviously, we can't use the underscore, since it already has a
meaning.  I can't think of a nice symbol off the top of my head that isn't
already used (period/full stop would be my next choice, but it's obviously
problematic too.  Maybe asterisk?)

Anyway, it would allow the original example to be rewritten like:

foo(Value) ->
	bar(Value),
	baz(*),
	zab(*),
	{ok, *}.

Which is, to me, clearer than the DCG approach.

No sneaky claiming this is single-assignment, anymore, though :)

-Chris



More information about the erlang-questions mailing list