From bug-request at octave dot org Fri Feb 4 11:03:43 2005 Subject: Re: Should be able to say 'for (k=1:N)' From: "John W. Eaton" To: Paul Kienzle Cc: Julius Smith , bug@octave.org Date: Fri, 4 Feb 2005 11:07:56 -0600 On 4-Feb-2005, Paul Kienzle wrote: | This is not a parser inconsistency. | | "for k = ..." is special syntax in the language. | | The consistent thing is to consider (k=1:3) to be a value. | | Whatever you expect from the statement: | | for 1:3, ... end | | you should get the same thing from | | for (k=1:3), ... end | | The following does look weird, but it is consistent: | | for k=x=1:3, k, x, end | | It assigns x to [1,2,3], then lets k cycle over x. Yes, this is all correct. However, Matlab does seem to allow for VAR = VAL ... end and for ( VAR = VAL ) ... end Both are apparently special forms in the language because it does not allow for ( ( VAR = VAL ) ) ... end I think this is consistent behavior in Matlab. It does not allow a = b = c; because an assignment is a statement, not an expression that produces a value. I admit that I have never before seen anyone use for (k = 1:N) ... in Matlab code, so I would guess it is relatively rare. The change I made to handle this form only allows the additional special case for ( VAR = VAL ) ... end to work. Note that currently for VAL ... end is not valid, and for VAR = VAL ... end with VAL being an assignment expression still works after my change, so your for k=x=1:3 k, x, end code will still work. I'm in favor of keeping the change for compatibility, but am open to other suggestions. 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 -------------------------------------------------------------