Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Question regarding Mathematica's treatment of whitespace

55 views
Skip to first unread message

Matt

unread,
Oct 30, 2005, 1:01:04 AM10/30/05
to
Hello,
It seems that most of the time Mathematica ignores whitespace. e.g. all of
the following evaluate with no problem:

Sin [
Pi/2
]


Sin [

Pi/2

]


However,

Sin
[Pi/2]

does not. Is that just the way it is? The reason I ask, is because I
would like to be able to use brackets '[,]' as I do braces in C/C++ to
help show the 'flow' of logic (within larger functions obviously), but
obviously Module[] doesn't work if it sees a cr/lf before the opening
bracket.

Thanks,

Matt

Chris Chiasson

unread,
Oct 30, 2005, 5:55:39 AM10/30/05
to
I don't know how to do it, but it may be easier to change the way that
the Mathematicia Front End automatically indents/spaces code to your
"one true braces form" (to quote Joel Spolsky without looking up the
real quotation...)


--
http://chrischiasson.com/contact/chris_chiasson

David Bailey

unread,
Oct 31, 2005, 1:17:59 AM10/31/05
to
Hello,

I think this strange result is caused by the fact that Mathematica will
treat a cell consisting of several lines as distinct inputs to
Mathematica UNLESS it finds certain pieces of syntax, such as open
brackets or binary operators requiring an RHS, that indicate otherwise.

Try executing the following 2-line cell:

2
3

The answer comes back as 3, because 2 is 'evaluated' and the result
discarded, and then 3 is 'evaluated'.

However, either of the following cells evaluate as one expression:
(
2
3
)

or

2*
3

Thus the following cell evaluates successfully as one expression:

(
Sin
[Pi]
)

So if you typically write your code inside Module/Block this will not be
a problem.

David Bailey
http://www.dbaileyconsultancy.co.uk

Steven T. Hatton

unread,
Oct 31, 2005, 1:18:45 AM10/31/05
to
Chris Chiasson wrote:

> I don't know how to do it, but it may be easier to change the way that
> the Mathematicia Front End automatically indents/spaces code to your
> "one true braces form" (to quote Joel Spolsky without looking up the
> real quotation...)

The rule Mathematica uses to determine if input is terminated by and end of
line marker, which I suspect is '\n', is whether the line constitutes a
syntactically complete expression myFunction[x_]:=Module\n does constitute
such an expression. This may not seem intuitive to people accustomed to
strongly typed and highly structured languages such as C or C++. One
alternative is to use
myFunction[xx_]:=
Module[
{
x=xx;
}.
DoSomething[x]
]

I really wish there were better syntax highlighting, particularly with
parentheses. Trying to sort out where things begin and end in deeply
nested boxes can be maddening. Forget for the moment that Mathematica
doesn't provide the named entity references in MathML when you copy from a
cell, and consider who much easier it is to find the ends of nesting
component in the following (admittedly verbose) representation:

<math xmlns='http://www.w3.org/1998/Math/MathML'>
<mn>5) </mn>
</math>
<math xmlns='http://www.w3.org/1998/Math/MathML'>
<mrow>
<mrow>
<mi>M</mi>
<mtext> </mtext>
<mo>&#8834;</mo>
<mtext> </mtext>
<mi>&#8469;</mi>
</mrow>
<mtext> </mtext>
<mo>&#8717;</mo>
<mtext> </mtext>
<mrow>
<mrow>
<mn>0</mn>
<mtext> </mtext>
<mo>&#8712;</mo>
<mtext> </mtext>
<mi>M</mi>
</mrow>
<mtext> </mtext>
<mo>&#8743;</mo>
<mtext> </mtext>
<mrow>
<mo>(</mo>
<mrow>
<mrow>
<mi>m</mi>
<mo>&#8712;</mo>
<mi>M</mi>
</mrow>
<mtext> </mtext>
<mo>&#62755;</mo>
<mtext> </mtext>
<mrow>
<msup>
<mi>m</mi>
<mo>&#8242;</mo>
</msup>
<mo>&#8712;</mo>
</mrow>
</mrow>
<mo>)</mo>
</mrow>
</mrow>
</mrow>
</math>


Put that in your FrontEnd and `C-E' the cell (IOW, Ctrl+Shift-e). It often
happens that Mathematica doesn't correctly format expressions similar to
this, and I am forced to go in an untangle it. That is not my idea of fun.
--
"Philosophy is written in this grand book, The Universe. ... But the book
cannot be understood unless one first learns to comprehend the language...
in which it is written. It is written in the language of mathematics, ...;
without which wanders about in a dark labyrinth." The Lion of Gaul

bsye...@gmail.com

unread,
Oct 31, 2005, 6:13:36 AM10/31/05
to

Hi Chris,
Although not you wanted, but you can always triple left-click with the mouse
on a parenteses or brackets (curly / square) and the front end will select
the relevant code covered by it. Also if you triple left-click on a head of
an expresion it will select all the code that is enclosed by this head.
Until a decent coloring will be implemented in the front end (I believe it
will) you can use this property
yehuda

On 10/31/05, Steven T. Hatton <hat...@globalsymmetry.com> wrote:


>
> Chris Chiasson wrote:
>
> > I don't know how to do it, but it may be easier to change the way that
> > the Mathematicia Front End automatically indents/spaces code to your
> > "one true braces form" (to quote Joel Spolsky without looking up the
> > real quotation...)
>

Igor Antonio

unread,
Nov 1, 2005, 12:49:19 AM11/1/05
to
Matt wrote:
> Hello,
> It seems that most of the time Mathematica ignores whitespace. e.g. all of
> the following evaluate with no problem:
>
> Sin [
> Pi/2
> ]
>
>
> Sin [
>
>
>
> Pi/2
>
>
>
> ]
>
>
> However,
>
> Sin
> [Pi/2]
>
> does not. Is that just the way it is? The reason I ask, is because I
> would like to be able to use brackets '[,]' as I do braces in C/C++ to
> help show the 'flow' of logic (within larger functions obviously), but
> obviously Module[] doesn't work if it sees a cr/lf before the opening
> bracket.
>
> Thanks,
>
> Matt


You're correct that Mathematica ignores most whitespaces, but Mathematica will
treat different lines as different expressions, unless there are
brackets/parentheses/curly braces/operators/etc that indicate that the content
continues on the next line.

Here's a similar example:

In[13]:= 1 +
1

Out[13]= 2

In[14]:= 1
+1

Out[14]= 1

Out[15]= 1

You have to have something that indicates the expression is not complete on the
current line. In your case, you'll have to use the brackets. The way I write
to show the "flow" of logic is:

f[x_] := Module[{},
...
]

or sometimes

f[x_] :=
Module[{},
...
]

(if I want the name of the function on its own line)

When "looking" at the code, I can quickly match the closing bracket of Module
with the function f.
--


Igor C. Antonio
Wolfram Research, Inc.
http://www.wolfram.com

To email me personally, remove the dash.

car...@colorado.edu

unread,
Nov 2, 2005, 4:22:33 AM11/2/05
to
Mathematica should mandate ; as terminator of any expression,
as in C. Then problems such as the one noted by the OP
would disappear: with CR/LF uniformly ignored source interpretation
becomes more linebreak independent.

Also the common warning about "this statement
will be interpreted as implicit multiplication" in modules, with
consequent risk of infinite recursion, would go away.

To get an expression printed a short construct such as

a = Sin[Pi/2] >>;

which outputs "a=1" could be introduced to avoid the
verbosity of Print. This could be easily edited in and out in
module debugging.

Murray Eisenberg

unread,
Nov 3, 2005, 5:15:16 AM11/3/05
to
But in Mathematica, the semicolon is NOT a terminator at all. Rather,
it's a SEPARATOR -- between parts of a CompoundExpression. (Something
of the form "a;b;...", etc.)

When the user omits the final statement in the compound expression, as
in "a;b;" then Mathematica inserts Null as that missing final statement.
Since Null is normally not printed as a result, the effect is to
display no output whatsoever.

It's as sensible to insist that Mathematica "should" adopt this or that
syntactical convention of some other programming language, as to insist
that, say, English should obey the syntax rules of some other natural
language.

--
Murray Eisenberg mur...@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

car...@colorado.edu

unread,
Nov 4, 2005, 5:34:36 AM11/4/05
to
I know how things works now. There is no need to repeat the
manual. My suggestion is to require an explicit terminator.

Present interpretation rules stem from history. Before there was a
front end, Mathematica and its ancestor SMP were kernel
only programs, executed by the usual methods. Type something,
hit CR, watch the response. Command languages have
operated that way since CTSS in 1962.

Want to type several commands in one line? Separate them by
a reserved symbol ( ; and $ have been favorites since
1970), hit CR. The compound command was born.
Want to reuse a block of commands? Collect somewhere,
give a label, type label, hit CR. The runstream was born.
(I think it came first in Univac's TSS).

With the advent of the front end a block organization becomes
more natural and Mathematica gradually morphs into a scripting
and programming language. However remnants of the old CL
style still lurk behind. Those can be noticed, for example,
in the In[] and Out[] labels that clutter notebooks and
baffle my students.

A mandatory terminator would eliminate ambiguities, and
be especially beneficial in Modules and Blocks.
The ancient rules could be left active for direct kernel
execution, which is nowadays rare by end users.

Chris Chiasson

unread,
Nov 5, 2005, 2:04:49 AM11/5/05
to
Dear Carlos,

I like it better without a mandatory terminator. I also happen to
think that a terminator is an artifact of procedural programming.
Think about how ridiculous a cell expression would look if it were
required to have a mandatory terminator.

Cell[BoxData[
RowBox[{"1", "+",
RowBox[{"2", "mandatoryterminator"}]}]], "Input"]

versus the way it is now

Cell[BoxData[
RowBox[{"1", "+", "2"}]], "Input"]

As you were probably already aware, there are plenty of things that go
after the two, which the user does not need to look at and that do
"terminate" the input expression.

Regards,


--
http://chrischiasson.com/contact/chris_chiasson

0 new messages