[erlang-questions] Keyword documentation

Jay Nelson jay@REDACTED
Fri Feb 19 19:12:25 CET 2010


tsuraan wrote:

 > begin
Start a block, used for grouping expresssions (no idea what use it is)

---------------------

Most common uses I've found are in list comprehensions:

[ X || X <- List,  begin FooVal = foo(X), ... complex test using  
FooVal more than once ... end ].

[ begin
        FooVal = foo(X),
        ... complex expression using FooVal more than once ...
   end || X <- List ].



It lets you introduce temporary variables in places where the context  
makes it difficult to reference them.
I do this for documentation purposes sometimes (see R.O.K.s previous  
objection to F local fun var), and when a call to an expensive  
function needs to occur more than once in a decision process.


Also works in binary comprehensions:

<<  << X >> ||  << X >> <= Binary,  begin FooVal = foo(X), ... end >>.

<<  <<(begin FooVal = foo(X), ... end)>> || << X >> <= Binary >>.

The parens are necessary because the expression in the binary  
constructor has to be evaluated and returned as a valid element  
before the binary construction can occur.

The value of a begin ... end construct is the value of the last  
statement.


jay




More information about the erlang-questions mailing list