Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Whether for nested Ifs or within scoping constructs like Module and Block, I never know whether to end the final expression with a semicolon. I know the semicolon suppresses output, but, for example, in a series of nested Ifs, one needs only to semicolon the outermost If to suppress output.

So, is it better practice to semicolon everything unless output is needed? Or is it better to semicolon nothing until the last point where output is unwanted?

share|improve this question
2  
I tend to put it everywhere I don't expect desired output: saves me from the "where'd that Null come from" and the "surprise multiplications" problems... –  rasher Jan 25 at 7:13
    
This clearly depends on the expression. For example, you can't just write If[x < 1, y = 1 z = 3 , y = 4 ] without a ; between y=1 and z=3. Even if you have the z=3 in a new line in the notebook. One still needs a ; –  Nasser Jan 25 at 7:28
1  
@Nasser - That's why I said the "final" expression—I mean the last expression before exiting a bracket. –  Andrew Cheong Jan 25 at 7:39
2  
You might find this answer helpful. –  m_goldberg Jan 25 at 14:18
    
@m_goldberg I forgot about that answer! Do you think this should be closed as a duplicate? –  Mr.Wizard Jan 25 at 18:23
show 2 more comments

1 Answer

up vote 14 down vote accepted

I may be missing the point of this question, but I think it is important to note that ; is the short form of CompoundExpression, and it is not primarily for suppressing output.

You can see how ; is interpreted using one of the methods I described here:

HoldForm @ FullForm[a; b; c]
HoldForm @ FullForm[a; b;]
CompoundExpression[a, b, c]
CompoundExpression[a, b, Null]

Note that when a final expression is omitted Null is inserted, just like this behavior with ,:

{1, 2,}
{1, 2, Null}

The suppression of output is not a behavior of CompoundExpression but rather Null, which when returned as output (alone) is not printed. For example 2 Null/2 evaluates to Null, therefore when given as input no output is printed.

There is little point in sprinkling ; around your code without need, unless perhaps as a visual reminder that the evaluated form of a given expression is not directly used by the surrounding head. One exception is when writing code that will eventually be made into a function or Module as line breaks are not valid separators in this case, therefore even functions that already return Null such as SetDelayed should be terminated with a ;, e.g.:

foo[bar_Integer] := bar^2;
foo[bar_Real]    := bar/2;

This avoids the error seen with:

Module[{foo},
  foo[bar_Integer] := bar^2
  foo[bar_Real]    := bar/2
]

What ; is really for

The actual function of CompoundExpression is described in the documentation:

expr1;expr2; ... evaluates the expri in turn, giving the last one as the result.

CompoundExpression, while more succinct and canonical than other methods, is not the only way to accomplish this. For example you could use

Last[{expr1, expr2, ...}]
in the same manner, e.g.:

a = 5; b = a^2; Binomial[b, a]
Last[{a = 5, b = a^2, Binomial[b, a]}]
53130

53130

And suppression of output:

x = Range@500;           
Last[{x = Range@500,}]   (* no output printed *)

You can also return a specific expression using Slot in a simple Function:

#2 &[a = 5, b = a^2, Binomial[b, a]]
25

This is specifically appropriate when you need to perform some action after generating an expression, such as closing a stream:

str = StringToStream["abcdefg 123456"];
# &[Read[str, Word], Close @ str]
"abcdefg"

I would be remiss not to mention that these alternative methods are not actually equivalent to CompoundExpression because even though they only return one expression (possibly Null) they still accumulate all of them in memory. Compare these, each pair of lines run in a fresh Kernel:

Range@1*^7; Range@1*^7; Range@1*^7; Range@1*^7;
MaxMemoryUsed[]
94923152
Last[{Range@1*^7, Range@1*^7, Range@1*^7, Range@1*^7,}]
MaxMemoryUsed[]
174925336

To get the memory performance of ; one would need something more complex, such as:

SetAttributes[ce, HoldAll]
ce[x__] := Fold[#2 &, , Hold @ x]

ce[Range@1*^7, Range@1*^7, Range@1*^7, Range@1*^7,]
MaxMemoryUsed[]
93506816
share|improve this answer
2  
So when are you going to write that Mathematica book. I would love to read it :) –  RunnyKine Jan 25 at 18:49
3  
    
Please, I'm curious to see the evolution of this amazing tool from someone who has studied it well before it became available :) –  RunnyKine Jan 25 at 19:09
    
@RunnyKine In case you didn't get the reference: imdb.com/title/tt0088323 -- worth a viewing, even as an adult. (And my point was: when I've been using Mathematica that long I may finally be qualified to write a book about it.) –  Mr.Wizard Jan 25 at 19:20
    
I've seen it a few times when I was much younger :). In any case, I'm pretty sure you're more than qualified to write one. –  RunnyKine Jan 25 at 19:27
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.