Why is a trailing comma in a cell array valid Matlab syntax?

后端 未结 3 573
囚心锁ツ
囚心锁ツ 2021-02-05 09:36

I was surprised today to discover that

A = {1,2,3}

and

B = {1,2,3,}

are both valid syntax in MATLAB. I would

3条回答
  •  误落风尘
    2021-02-05 10:23

    Many languages allow one extra element separator in lists, as already mentioned. But this has nothing to do with run-time parsing. Even C allows it. It has to do with ease of use. This is a feature meant to help the user. For example, in C you can define an enum as follows:

    enum E {
       a,
       b,
       c,
    };
    

    The comma after c is not required, but it is allowed. It makes it easier to add and remove elements from such a list, and it makes it easier to programatically generate such a list (mlepage's answer is correct!).

    So, allowing one additional comma at the end is common across most (if not all) programming languages, it makes sense for MATLAB to support it too. The additional comma at the beginning of the list makes less sense, but I guess they support it because it doesn't hurt either.

    Multiple commas in a row make no sense, this would imply there are additional elements that are unspecified.

    But what is going on with the multiple semicolons then? As Luis Mendo mentioned, [1;;2] is legal syntax. This is something that does deviate significantly from what other languages do.

    However, it is consistent with MATLAB's use of the line break. In MATLAB, line breaks are significant. When defining an array using [], line breaks indicate new rows of data:

    M = [ 1, 2, 3,
          4, 5, 6,
          7, 8, 9,
        ];
    

    is the same as

    M = [1,2,3; 4,5,6; 7,8,9];
    

    (Note how allowing the commas at the end of each row can be convenient at times.)

    (Also note that I use [] here to concatenate, the exact same logic applies to {}.)

    But because MATLAB wants to allow as much as possible, as long as it remains unambiguous, the above is the same as:

    M = [ 1, 2, 3,
          4, 5, 6,
    
    
          7, 8, 9,
        ];
    

    If it doesn't hurt to allow empty lines, why not allow them?

    Since each newline corresponds to a semicolon, the above is identical to:

    M = [ 1, 2, 3,;...
          4, 5, 6,;...
                  ;...
                  ;...
          7, 8, 9,;...
        ];
    

    which is identical to

    M = [ 1, 2, 3,; 4, 5, 6,; ; ; 7, 8, 9,; ];
    

    and so MATLAB must be able to parse that, whether it makes sense or not.


    A rebuttal of thewaywewalk's answer:

    The argument is that both , and ; are defined as statement separators, but somehow it is assumed that ;;; is a valid statement whereas ,,, is not. This is simply not true:

    disp(0),,,disp(1)
    disp(0);;;disp(1)
    

    are both valid MATLAB syntax (R2017a parses both without error).

    Furthermore, the answer confuses expressions and statements. disp(0) is a statement. The 0 in that statement is an expression. In M=[1,2,3], the things separated by commas are expressions, not statements. The commas there do not work as statement separators.

    In fact, in MATLAB the comma and the semicolon have multiple meanings, depending on context. The comma and semicolon at the end of a statement (including a null statement) is different from the comma and semicolon within a concatenating expression ([1,2;3,4]). And the comma can also separate expressions within the brackets of a function call, where the semicolon is not allowed.

    Just to make this point clear:

    1,,,4
    

    is valid, whereas

    [1,,,4]
    

    is not. The commas have different functions in these two statements.

    In short, the logic used in that answer is simply incorrect.

提交回复
热议问题