Comments on: Interesting Matlab puzzle https://undocumentedmatlab.com/blog_old/interesting-matlab-puzzle Charting Matlab's unsupported hidden underbelly Thu, 07 Sep 2023 16:43:44 +0000 hourly 1 https://wordpress.org/?v=4.4.1 By: Brad Stiritzhttps://undocumentedmatlab.com/blog_old/interesting-matlab-puzzle#comment-473457 Mon, 08 Apr 2019 02:00:09 +0000 https://undocumentedmatlab.com/?p=8614#comment-473457 Hi Yair, I would expect the conditional expression to fail during parsing, and therefore lead to “Doo!” output per catch block. The proper “functional” syntax for logical OR is as follows:

>> or(false,true)
ans =
  logical
   1
]]>
By: Marshallhttps://undocumentedmatlab.com/blog_old/interesting-matlab-puzzle#comment-472724 Thu, 04 Apr 2019 17:39:28 +0000 https://undocumentedmatlab.com/?p=8614#comment-472724 (I meant variant #5)

]]>
By: Marshallhttps://undocumentedmatlab.com/blog_old/interesting-matlab-puzzle#comment-472722 Thu, 04 Apr 2019 17:38:40 +0000 https://undocumentedmatlab.com/?p=8614#comment-472722 Ha, it looks like variant #4 got me. Quite clever. When pasted in Matlab it’s a bit more obvious because the “10< 9.9" is coded purple, indicating character arrays.

]]>
By: Marshallhttps://undocumentedmatlab.com/blog_old/interesting-matlab-puzzle#comment-472721 Thu, 04 Apr 2019 17:35:38 +0000 https://undocumentedmatlab.com/?p=8614#comment-472721 It looks like this is just syntax muddling. Rewritten with better formatting, we get this:

function test
    try
        if(false)
            or(true);
            disp('Yaba');
        else
            disp('Daba');
        end
    catch
        disp('Doo!');
    end
end

We never actually reach the line “or(true)”, which would throw an error, causing the catch block to print ‘Doo!’, so instead we simply print ‘Daba’. The parentheses are required in order to have the (false) alone apply to the if statement, as opposed to what comes after.

Regarding the other questions:

if (false) or (true) % variant #1
if (true) or (false) % variant #2 — my guess is this will print ‘Doo!’, since the or(false) will throw an error
if (true) or (10< 9.9) % variant #3 — will also print ‘Doo!’ for the same reason: “or(10<9.9)” is invalid
if true or 10< 9.9 % variant #4 — will also print ‘Doo!’, but this time it’s because “true or 10< 9.9” or: “10>9.9 or 10<9.9” is invalid — the or function requires 2 arguments

]]>
By: Michelle Hirschhttps://undocumentedmatlab.com/blog_old/interesting-matlab-puzzle#comment-472001 Mon, 01 Apr 2019 15:57:49 +0000 https://undocumentedmatlab.com/?p=8614#comment-472001 Is it cheating if I not only work at MathWorks, but asked the development lead for the MATLAB Front End and Code Analyzer for an answer? Andrew emailed me the same puzzle over the weekend, which I forwarded on to the dev lead. Here’s the scoop:

For backwards compatibility, space is a valid separator between the expression in the if-condition and the first statement in the body. We don’t want to encourage users to write more such code because we realize this can be very confusing, but we also don’t want to break existing code which is out there and works correctly.

Therefore, the Code Analyzer tries to provide an indication of the current behavior and direct users away from it. For this specific code example, it provides these two messages on the “or” in the MATLAB Editor:

This statement (and possibly following ones) cannot be reached.
Consider using newline, semicolon, or comma before this statement for readability.

]]>
By: Willhttps://undocumentedmatlab.com/blog_old/interesting-matlab-puzzle#comment-471925 Mon, 01 Apr 2019 09:57:06 +0000 https://undocumentedmatlab.com/?p=8614#comment-471925 Oh dear, seems I was wrong on (at least) two counts above…

First, the first set of brackets seem to do nothing. I was sure you normally needed a comma after the condition expression, but apparently not?!

Second, the command in variant #4 still doesn’t qualify as part of the if condition. So after having satisfied the if true condition, first the result of the unsuppressed command or 10< 9.9 is displayed in the command window:

ans =
  1×3 logical array
   1   1   1

then ‘Yaba’ is displayed!

]]>
By: Willhttps://undocumentedmatlab.com/blog_old/interesting-matlab-puzzle#comment-471920 Mon, 01 Apr 2019 09:30:43 +0000 https://undocumentedmatlab.com/?p=8614#comment-471920 Very intriguing puzzle! I never knew that parentheses around the expression immediately following if allowed you to continue with the first line of the statement group without a comma or semicolon. Is this syntactic logic really not documented? I certainly couldn’t find any.

That behaviour is of course the reason the main puzzle results in Daba, after seeing that the full condition of the if statement is just false. I don’t think that an if statement skipping the block it would execute if true and proceeding to the else block can be described as short-circuit evaluation, but maybe my understanding of execution engine theory falls short here.

The variants are fun, too.

In variant #2 we pass the if condition and get to the function call to or (disguised as a binary operator to fool users of more verbose languages than MATLAB) with only one argument – throwing an exception that results in a Doo!

In variant #3 nothing changes, because of course 10< 9.9 just evaluates to a scalar false which still isn’t an adequate input to the or function. But of course this is just a set up for

variant #4, which cunningly uses command syntax to pass two character vector arguments to or, each carefully engineered to be three characters long so that we evaluate the equivalent of ’10<‘ | ‘9.9’ which is a valid vector boolean operation. Since none of the characters involved is the null character the result is `[true true true]` so we finally get a `Yaba`.

Happy April Fools’!

]]>
By: Codrut Danhttps://undocumentedmatlab.com/blog_old/interesting-matlab-puzzle#comment-471884 Mon, 01 Apr 2019 07:04:03 +0000 https://undocumentedmatlab.com/?p=8614#comment-471884 Hi everyone,

new here :)

nice puzzle. I have never used ‘or’ like that, only used function or(A,B), or the || operator. I would have expected error there…so that was my answer :)
Now I’ll need to review my knowledge on syntax…

]]>
By: Rik Wisselinkhttps://undocumentedmatlab.com/blog_old/interesting-matlab-puzzle#comment-471713 Sun, 31 Mar 2019 16:54:18 +0000 https://undocumentedmatlab.com/?p=8614#comment-471713 Prior to running it, I would have expected the if to error. Then I pasted it into the editor and the lint told me to put a comma after the false. That caused me to believe it saw the false as the complete conditional, which would cause the else to be executed (which seems to be the case when running it).

I suspect the reason for this stems from the support for a single line if. I suspect Matlab parses everything from the if to the end of the first function call (or of course || or &&) as the conditional.

%triggers the else branch:
if (true && false) or (true)
%  ^. . . . . . .^
%this is parsed as the conditional (space delimited command)
if (true && false) or (true)
%                  ^. . . .^ 
%This is considered the code to be executed on if, because there is no
%logical operator there, and the preceding part is a complete command.
%Removing the parentheses there will not make a difference, as the space is
%still demarcating the logical statement from the consequence code.

Replacing the true and if statements by anything that returns those statements does not change the parsing. Removing the parentheses will only change the or() call to be in the char mode, which would then still be missing its second input. Curiously, you can’t enter a false in char mode. If you try with eval to have a char(0) to force a false, an error is returned instead (not enough inputs), as eval seems to crop from char(0) onwards.

]]>
By: Roger Watthttps://undocumentedmatlab.com/blog_old/interesting-matlab-puzzle#comment-471709 Sun, 31 Mar 2019 16:36:44 +0000 https://undocumentedmatlab.com/?p=8614#comment-471709 this one works as expected:

if (false) || (true)
etc

the parsing is:

if false,
 or(true)
 disp(1)
else
 disp(2)
end
]]>
By: Vinhttps://undocumentedmatlab.com/blog_old/interesting-matlab-puzzle#comment-471684 Sun, 31 Mar 2019 14:27:16 +0000 https://undocumentedmatlab.com/?p=8614#comment-471684 Your answer is doo-doo!

]]>
By: Vinhttps://undocumentedmatlab.com/blog_old/interesting-matlab-puzzle#comment-471683 Sun, 31 Mar 2019 14:23:16 +0000 https://undocumentedmatlab.com/?p=8614#comment-471683 The answer is Doo!

]]>
By: Cris Luengohttps://undocumentedmatlab.com/blog_old/interesting-matlab-puzzle#comment-471680 Sun, 31 Mar 2019 14:09:06 +0000 https://undocumentedmatlab.com/?p=8614#comment-471680 Great Sunday morning exercise! I’m curious to see what happens when I type this into MATLAB tonight. :)

]]>
By: Cris Luengohttps://undocumentedmatlab.com/blog_old/interesting-matlab-puzzle#comment-471677 Sun, 31 Mar 2019 14:06:42 +0000 https://undocumentedmatlab.com/?p=8614#comment-471677 or(true) and or true both are missing an argument, I would guess. or true is the same as or('true'), it passes a char array as argument. The function needs two arguments. If executed they’d throw an error. So starting with if true would cause the catch to execute.

I don’t know what happens with the parenthesis around the argument to if, maybe they’re needed to prevent a syntax error because of the missing comma, but this is just a guess.

]]>
By: Yair Altmanhttps://undocumentedmatlab.com/blog_old/interesting-matlab-puzzle#comment-471672 Sun, 31 Mar 2019 13:47:34 +0000 https://undocumentedmatlab.com/?p=8614#comment-471672 So Cris – what would you expect to happen in Matlab with the variants above? (code as-is; switching false<=>true; and without parentheses)

]]>
By: Cris Luengohttps://undocumentedmatlab.com/blog_old/interesting-matlab-puzzle#comment-471669 Sun, 31 Mar 2019 13:37:25 +0000 https://undocumentedmatlab.com/?p=8614#comment-471669 This looks like a Python programmer tried writing MATLAB code. OR is a function, not an operator.

Does MATLAB interpret this as

if (false), or (true)

?

]]>
By: braggpeakshttps://undocumentedmatlab.com/blog_old/interesting-matlab-puzzle#comment-471665 Sun, 31 Mar 2019 13:21:10 +0000 https://undocumentedmatlab.com/?p=8614#comment-471665 So, back from lunch 😉

The parser seems to interpret “or” different to | and ||, i.e. as a new command. or(true) would then cause an error, but the line is not reached. The same would apply for “and“.

I guess why it is interpreted differently is the big Q – I have no answer to that 😉

]]>
By: Yair Altmanhttps://undocumentedmatlab.com/blog_old/interesting-matlab-puzzle#comment-471638 Sun, 31 Mar 2019 11:16:02 +0000 https://undocumentedmatlab.com/?p=8614#comment-471638 I said that it’s not as trivial as it might look at first glance :-)

]]>
By: braggpeakshttps://undocumentedmatlab.com/blog_old/interesting-matlab-puzzle#comment-471637 Sun, 31 Mar 2019 11:10:48 +0000 https://undocumentedmatlab.com/?p=8614#comment-471637 hold on, that was way too quick

]]>
By: braggpeakshttps://undocumentedmatlab.com/blog_old/interesting-matlab-puzzle#comment-471630 Sun, 31 Mar 2019 10:43:43 +0000 https://undocumentedmatlab.com/?p=8614#comment-471630 – or is logical OR, thus no parsing error or ‘Doo!’
– false is interpreted first, so result would be ‘Daba’

]]>