Wolfram: CompoundExpression and Semicolon
One single semicolon in WolframLang, e.g.
a;b
is a short syntax for
CompoundExpression[a, b]
It is used to group several expressions together as one syntactic unit.
It is similar to many language's code block by braces {…}, or lisp's progn.
| Short syntax | FullForm |
expr1;expr2 | CompoundExpression[expr1, expr2] |
expr1; | CompoundExpression[expr1, Null] |
CompoundExpression[expr1, expr2]-
🔸 SHORT SYNTAX:
expr1;expr2Eval all arguments and return the value of last argument.
Examples of CompoundExpression
CompoundExpression
is frequently used in
If,
Function,
Module,
or anytime when you need to do several computation as a single expression as one arg to function.
Examples:
If[x,
doTrueExpr1;
doTrueExpr2;
etc,
doFalseExpr1;
doFalseExpr2;
etc
]
〔see Wolfram: If Then Else (Conditional)〕
Function[{vars},
expr1;
expr2;
etc]
〔see Wolfram: Function〕
Module[{vars},
expr1;
expr2;
etc
]
〔see Wolfram: Local Variable〕
Often,
you'll find it convenient to put a semicolon at end of some expression.
In particular, after assignment.
It has the effect of suppressing output.
For example, x=3 return 3, but x=3; return Null, and Null output is not automatically printed.