[erlang-questions] is there "return" in Erlang.

Jachym Holecek freza@REDACTED
Mon Feb 28 16:29:02 CET 2011

# Frédéric Trottier-Hébert 2011-02-28:
> On 2011-02-28, at 09:18 AM, Jachym Holecek wrote:
> > # Attila Rajmund Nohl 2011-02-28:
> > So "special" constructs in Erlang, like 'case' or 'if', are really just
> > syntactic sugar on top of lambdas (the strange scoping rule of 'case'
> > breaks the most straightforward approach to this, but can still be dealt
> > with cleanly).
> > 
> > I think this is a very powerful property, although as you can see I have
> > a bit of a trouble in clearly expressing why is that. ;-)
> This is not exactly true. The 'case ... of' expression has special scoping
> rules that do not fit those of lambdas or functions in general:

Yes, I've mentioned this explicitely.

> 1> case 1 of _ -> R = 3 end.
> 3
> 2> R.
> 3
> Compare with a fun/lambda:
> 3> (fun(_) -> D = 3 end)(1).
> 3
> 4> D.
> * 1: variable 'D' is unbound

Compare with:

  R = (fun (_) -> 3 end(1))

It's a slightly more involved transform in that you have to push variables
bound in case's branches into its return value automatically, but in priciple
it still works.

> And the 'if' expression follows the same pattern. Moreover, in a fun, you
> will 'shadow' the variables in the head, meaning you'll overwrite them if
> they existed in the current context. A case expression won't -- using the
> same binding of 'R = 3' as above:
> 5> case 2 of R -> yay end.
> ** exception error: no case clause matching 2

Yes, so you'll be careful to disambiguate variables in that case.

> From what I heard, the underlying dispatching principle is similar, but yeah.
> They're not equivalent in how they handle scoping or binding.

Which is something a sufficiently diligent code translator can deal with.

Don't forget my point is lambdas are, in principle, all you need to express
the remaining control flow constructs. I'm not saying it's a practical thing
to do, I'm saying that unlike in C you can isolate a basic building block
of the language, so as to say -- everything else revolves around it, so you
don't have to pollute your mind with an assortment of weird pieces that
sometimes fit together, but not quite all the time.

Hope this makes my previous post a bit clearer.

	-- Jachym

More information about the erlang-questions mailing list