help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Whitespace question, [zeros (1, 2), ones (1, 2)]


From: John W. Eaton
Subject: Re: Whitespace question, [zeros (1, 2), ones (1, 2)]
Date: Sat, 5 Nov 2005 22:19:49 -0500

On  5-Nov-2005, Mike Miller wrote:

| Interesting problem.  I do not get the parse error on this old version:
| 
| GNU Octave, version 2.0.14 (sparc-sun-solaris2.6)
| 
| But I do get it on this newer version:
| 
| GNU Octave, version 2.1.71 (i686-pc-linux-gnu)
| 
| John will probably have something to add.

Sure, Matlab makes whitespace significant in this context.  Octave
originally did not auto-insert a comma between an identifier and an
open paren because I didn't think to check to see what Matlab would do
in that case.  Later, when I found that Matlab parsed things like
"[ones (1, 2)]" as if they were written "[ones, (1, 2)]", I added a
built-in variable to allow users to specify which behavior they
preferred.  But that cure turned out to be worse than the disease, so
more recent versions of Octave dropped the optional behavior.

FWIW, I prefer to see code that looks like this

  fcn (x, y, z);

rather than any of the other variations I have seen, like

  fcn ( x, y, z );
  fcn( x, y, z );
  fcn(x, y, z);
  fcn(x, y, z);
  fcn(x,y,z);

The reason is that in normal written English (the language I'm most
familiar with) the convention is to have a space before an open paren
but not after it, and there are spaces after commas but not before
them.  It is harder for me to read code that does not use the same
convention for spaces because my eye expects to see the spaces as they
are in written English.  Oddly though, this does not seem to matter so
much to me when there are just array indices, so now I prefer to see

  x(i,j) = pi;

instead of

  x (i, j) = pi

(I think it helps if the variables used for the index are just i, j,
k, etc.).  Given that Octave uses the same syntax for function calls
and array element references, I now tend to use this difference in
whitespace to give myself a clue about the type of object that is
being indexed.  At least I find that it helps.  YMMV.

Back to the original question.  You have a couple of options other
than omitting the space in this context.  One is to use some temporary
variables and write

  t1 = ones (1, 2);
  t2 = zeros (1, 2);
  [t1, t2]

or you can use extra parens:

  [(ones (1, 2)), (zeros (1, 2))]

though neither is very satisfying to me.

FWIW, I think that making whitespace significant in this context was a
bad choice, but it seems that we are stuck with it.  Most Matlab users
that I know seem to love the feature that allows them to omit the
commas in statements like "[1 2 3]", so requiring commas would
probably not be a welcome change.  Getting the MathWorks to change the
behavior for the space-between-identifier-and-paren case is probably
just as unlikely.

Other oddities (mentioned in the Octave manual, I think):

  [1+ 2]  --> 3
  [1 + 2] --> 3
  [1 +2]  --> [1 2]

jwe



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]