MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

misunderstanding of semicolon

  • To: mathgroup at smc.vnet.net
  • Subject: [mg89494] misunderstanding of semicolon
  • From: Murray Eisenberg <murray at math.umass.edu>
  • Date: Wed, 11 Jun 2008 03:16:03 -0400 (EDT)
  • Organization: Mathematics & Statistics, Univ. of Mass./Amherst
  • Reply-to: murray at math.umass.edu

I think you may want to do some further study of Mathematica syntax, 
including special input forms and abbreviations and precedence. 
Semicolons are, in fact, well explained in the documentation, even in 
Mathematica 6.

Re your point 1): No, I believe you are mistaken.  If you look up 
semicolon ( ; ), for example by evaluating

   ?;

then you will see that it is used as a special input form that denotes 
CompoundExpression.

Now if you terminate an expression with a semicolon ...

   expr;

then that is interpreted as

   expr; Null

and the result -- as in ANY CompoundExpression -- is the result of the 
final one of the individual expressions in the list.  In this case that 
final one is Null, and ordinarily a result of Null by itself is 
suppressed in output.

This has long (always?) been the case in Mathematica.  It just didn't 
seem to matter as much until the fundamental behavior of the Plot family 
was changed in Version 6 so as to produce the graphic display itself as 
the direct result.

Whether a semicolon is used more frequently in Mathematica notebooks 
just in order to separate constituent expressions of compound 
expressions, on the one hand, or to have the EFFECT of suppressing 
output, on the other hand, is an empirical question we could only answer 
by some research.  But that has nothing to do with the actual meaning of 
the semicolon in the Mathematica language.

Now to your example:

    a = Plot[x, {x, 0, 1}];  b = Plot[x^2, {x, 0, 1}];  a  b

Yes, the a and the b at the end of this compound expression are indeed 
themselves expressions.  Indeed, EVERYTHING in Mathematica is an 
expression!  But the input a b is a shorthand (or maybe the technical 
term is "special input form"?) for the expression Times[a, b]; so is 
a*b.  Thus your compound expression really means:

    a = Plot[x, {x, 0, 1}];  b = Plot[x^2, {x, 0, 1}]; Times[a, b]

And, if you really wanted to go further, wrap that whole thing in 
FullForm@Hold to obtain:
 
CompoundExpression[Set[a,Plot[x,List[x,0,1]]],Set[b,Plot[Power[x,2],List[x,0,1]]],Times[a,b]]

Now it is totally unambiguous as to just what the three expressions are 
that form the overall compound expression.

As to your 2):  If you had not already encountered the semicolon in 
examples, you might find out about it in the Tutorial "Sequences of 
Operations".  In turn, you might come to that from the Overview 
"Building Up Calculations".

Or, you might start with the Documentation Center home page, and in the 
first category, "Core Langauge", follow the first link, which is to the 
Tutorial "The Synatx of the Mathematica Language", which is a single 
long page that around half-way down merely mentions compound expression 
(which you might follow up with an additional search).

Or, again starting from "Core Language", follow the "Syntax" entry link 
where, under the "Program Syntax" heading you'll find:

   expr;expr;expr \[LongDash] sequence of commands (CompoundExpression)

I'm sure there are other routes as well.

As to your 3):  If you go back and look at the flurry of queries about 
Show since Mathematica 6 appeared, I think you would be led inevitably 
to the conclusion that they are all engendered by folks upgrading from 
5.x to 6 and the changed behavior of the Plot family.

As to 4): I don't understand what the problem is here.  The single 
compound expression

   Print[Plot[x,{x,0,1}]];Print[Plot[x^2,{x,0,1}]];2+4

produces for me exactly the same display on screen as does a single 
input cell having the three separate input expressions:

   Plot[x,{x,0,1}]
   Plot[x^2,{x,0,1}]
   2+4

However, if you use your original example...

   Print[a = Plot[x, {x, 0, 1}]];  Print[b = Plot[x^2, {x, 0, 1}]];  a  b

... then indeed the size of the final graphic -- the ONLY one that's a 
direct result of the compound expression -- indeed has the two graphs 
displayed smaller.  And that's because Mathematica is displaying the the 
"product" of the two Plot objects.

As to 5), the key word in the quoted extract from the documentation form 
Show is "overlaying".  When there is a conflict between what should 
appear in a certain location according to one of the constituent 
graphics objects in the Show[g1, g2, ...] and another, then obviously 
some decision has to be made as which should dominate.  And "overlay" 
indicates a serial order of use.  Of course there's still some ambiguity 
here as to the order in which things are overlayed, but some examples 
quickly clear that up.  This is a point that could be made explicitly 
but, so far as I can see, is not made in the "More Information" for Show.


"AES" <siegman at stanford.edu> wrote in message 
news:g2ij15$rnk$1 at smc.vnet.net...
 > >I believe I've more or less fully grasped the explanation of what Show[]
 > > now does in 6.0 that's been repeatedly restated in all the recent and
 > > earlier responses to all the repeated plaintive posts about ""Why
 > > doesn't my plot appear?!?"  --- and this explanation actually make
 > > reasonable sense to me.
 > >
 > > [It's less obvious, however, how someone is suppose to know that
 > > Print[]; _will_ print something on screen, but Show[]; _won't_ show
 > > anything on screen.  Read the Helps for these two commands and see the
 > > help pages make this obvious to you)
 > >
 > > I'm less sure that these expert respondents -- and also WRI -- have
 > > grasped _why_ these recurrent queries keep recurring; so led me add a
 > > few (5 actually) responses myself on that point (though it will 
make for
 > > a long post):
 > >
 > > 1)  I suggest the primary problem is a deeper, long-standing, and quite
 > > understandable misunderstanding of compound expressions, and especially
 > > the role of semicolons in compound expressions.
 > >
 > > To quote, for example, from one of the recent "Plot and Show[]"
 > > responses, posted by someone who's often on this group:
 > >
 >> >> In general, a semi-colon is used to separate the parts of a compound
 >> >> expression
 > >
 > > Well, the correct response to that response is "NOT!", right?  Or at
 > > least, "not entirely".
 > >
 > > That is to say, is the primary function of a semicolon to separate the
 > > parts of compound expressions? -- or is it to suppress output from an
 > > expression?  And is it always necessary between two expressions?
 > >
 > > I've thought for all my years with Mathematica that you _had_ to put
 > > semicolons between two successive expressions on the same line or 
in the
 > > same cell, except for the very last line in the cell.  And, I guess I
 > > deduced that this made sense and was necessary because logically you
 > > always needed to be clear where one expression ended and the next one
 > > began.
 > >
 > > Given this, I would never have believed that giving as input, on a
 > > single line and in a single cell, the following
 > >
 > >   a = Plot[x, {x, 0, 1}];  b = Plot[x^2, {x, 0, 1}];  a  b
 > >
 > > would be a legal input -- until I tried it a couple of minutes ago.
 > >
 > > But wait a minute!  Aren't a and b now expressions (separately)?  So,
 > > don't they require a semicolon between them, to separate them?
 > >
 > > [In fact, I just tried the above input with  a + b  and then  a * b at
 > > the end of the line -- and they all worked exactly the same as just
 > > a b . WOW!!!]
 > >
 > > Semicolons need explaining and understanding!
 > >
 > > 2)  So, where would an innocent but intelligent Mathematica user go to
 > > unravel these mysteries.  I'd issue a small challenge:  Don't explain
 > > compound expressions and semicolons to me.  Show us -- show me -- in
 > > detail how a novice could learn the at least the essential elementary
 > > rules of compound expressions and semicolons FROM THE CURRENTLY
 > > AVAILABLE MATHEMATICA 6.0 DOCUMENTATION.
 > >
 > > [And, see if the Help for ";" comports with the results I just
 > > experienced in the preceding.]
 > >
 > > 3)  And then think a little deeper:  Ask yourself, WHY do these
 > > recurrent queries about Show[]keep recurring so recurrently?
 > >
 > > Could it just be because the _documentation_ provided for 6.0's
 > > introduction, and for the massive changes it introduced, has not been
 > > adequate in explaining or warning about these sizable changes for a
 > > large class of users???
 > >
 > > Let's just leave that thought to simmer for a while . . .
 > >
 > > 4)  OK, now having understood the new character of Show[], what should
 > > users do if they want to really show some graphics -- have them appear
 > > on screen -- in the middle of a long compound expression.
 > >
 > > Respondents keep suggesting that one should Print[] the graphics.  OK,
 > > that works, of course -- but it's a poor solution, among other things
 > > because Print[] messes with the sizes at which the graphics are
 > > displayed, in a way that doesn't always match with what's expected.
 > > Something better is needed.
 > >
 > > 5) Finally, to respond on a quite different aspect of Show[] that's 
also
 > > been the subject of a thread recently:
 > >
 > > Go to the Help for Show[], where you'll read:
 > >
 > >      Show[g1,g2]  . . . concatenates the graphics primitives in the
 > >      gi, effectively overlaying the graphics.
 > >
 > > and then try
 > >
 > >      a = Plot3D[x y, {x, 0, 1}, {y, 0, 1}];
 > >      b = Graphics3D[{Red, Thickness[0.01],
 > >             Line[{{1/2, 1/2, 0}, {1/2, 1/2, 2}}]}];
 > >
 > > followed by Show[a, b]  and then  Show[b, a].  Evidently,
 > > Show[a,b] =|= Show[b,a].
 > >
 > > I guess I grasp what happens here, don't need an explanation, and can
 > > sort of understand, yeah, that's what "concatenate" gives you.  But 
it's
 > > a lesson on how careful you have to be with these things.
 > >
 > > That's it for the 5.
 > >



-- 
Murray Eisenberg                     murray at math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower      phone 413 549-1020 (H)
University of Massachusetts                413 545-2859 (W)
710 North Pleasant Street            fax   413 545-1801
Amherst, MA 01003-9305


  • Prev by Date: Re: Adding markers on the surface of a Plot3D?
  • Next by Date: fit plane to xz axis of data
  • Previous by thread: Re: Transform list of numbers into pure functions using
  • Next by thread: fit plane to xz axis of data