##Understand that semicolon (;) is not a delimiter
Understand that semicolon (;) is not a delimiter
Although it may look to newcomers that semicolons are used in Mathematica as statement terminators as in C or Java, or perhaps as statement separators as in Pascal and its derivatives, in fact, semicolons are the infix form of the function CompoundExpression
, just as plus-signs (+) are the infix form of the function Plus
.
You can verify this by evaluating
Hold[a; b; c] // FullForm
Hold[CompoundExpression[a, b, c]]
CompoundExpression
is necessary to Mathematica because many of the core programming functions such as SetDelayed
(:=), Module
, Block
, and With
take only a single expression as their second argument. This second argument is of course the code body and normally requires the evaluation of many expressions. CompoundExpression
provides the construct that bundles an indefinite number of expressions into one.
Wolfram Research chose semicolon for the infix operator form of CompoundExpression
to make Mathematica code look more like C code, but this is only syntactic sugar.
The only true delimiter in Mathematica is comma (,).
###Update
Update
One often sees code like the following
data = RandomReal[{0., 10.}, {1000, 2}];
The variable data
is probably going to be used as test or example data. The semicolon is added to suppress the output from this Set
expression because the output is both very large and no one really cares about its details. Because there is no visible output, it would be easy to assume the expression returns nothing, but that is not true. Mathematica expressions always return something, even if it is only the token Null
, which does not print in OutputForm
. In the case of CompoundExpression
, there is an additional twist -- I quote from the Mathematica documentation:
expr1; expr2; returns value
Null
. If it is given as input, the resulting output will not be printed.Out[n]
will nevertheless be assigned to be the value of expr2.
This the only case I know of where evaluating an expression at toplevel doesn't assign its actual output to Out[n]
.
keywords delimiter terminator separator semicolon compound-expression