help-octave
[Top][All Lists]
Advanced

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

Re: cs-lists and multiple return arguments


From: John W. Eaton
Subject: Re: cs-lists and multiple return arguments
Date: Mon, 31 Oct 2011 01:49:28 -0400

On 30-Oct-2011, Jordi Gutiérrez Hermoso wrote:

| 2011/10/28 Jordi Gutiérrez Hermoso <address@hidden>:
| > On 28 October 2011 11:45, Przemek Klosowski <address@hidden> wrote:
| >> On 10/28/2011 02:26 PM, Jordi Gutiérrez Hermoso wrote:
| >>>
| >>> I don't understand why this works:
| >>>
| >>>     [a,b] = {1, 2}{:}
| >>>
| >>> but this doesn't
| >>>
| >>>     [a,b] = 1,2
| >>
| >> 1,2 is two statements; statement '2' doesn't do much while '1' tries to
| >> supply both results ('a' and 'b'), and fails.
| >>
| >> [a,b] = deal(1,2) works fine---that's what 'deal()' was intended for.
| >
| > Yeah, I know, but in every other situation I can think of, a cs-list
| > is syntactically identical to typing out its elements one by one. Why
| > doesn't that work here? Also, a common use of deal is like this:
| >
| >    [a b] = deal(c{:})
| >
| > but
| >
| >    [a b] = c{:}
| >
| > makes this completely superfluous. I am curious to know if [a, b] =
| > c{:} works in Matlab.
| 
| Seems like it also works in Matlab. I'm thoroughly confused as to why
| this works. I guess I'll just live with it working and start using
| this trick.

Because TMW decided it should work.  This change apparently happened
in Matlab version 7 (AKA R14):

  http://www.mathworks.com/help/techdoc/rn/f8-1009921.html#f8-1013499

It's sometimes convenient to think of operations which generate
comma-separated lists as syntactically equivalent to an actual
comma-separated list of expressions, but that's not really what those
expressions are doing.  What I mean is that given c = {1, 2}, the
interpreter does not take c{:} and generate the text "1, 2" and then
feed that back into the parser.  It just generates something that can
be used as a list of values in some special circumstances like
arguments to a function call or in assignments to multiple values.  If
you think it is inconsistent, blame TMW.

jwe


reply via email to

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