- Undocumented Matlab - http://undocumentedmatlab.com -

Interesting Matlab puzzle

Posted By Yair Altman On March 31, 2019 | 20 Comments

Here’s a nice little puzzle that came to me from long-time Matlab veteran Andrew Janke [1]:
Without actually running the following code in Matlab, what do you expect its output to be? ‘Yaba’? ‘Daba’? perhaps ‘Doo!’? or maybe it won’t run at all because of a parsing error?

function test
        if (false) or (true)

To muddy the waters a bit, do you think that short-circuit evaluation [2] is at work here? or perhaps eager evaluation? or perhaps neither?
Would the results be different if we switched the order of the conditional operands, i.e. (true) or (false) instead of (false) or (true)? if so, how and why?
And does it matter if I used “false” or “10< 9.9” as the “or” conditional?
Are the parentheses around the conditions important? would the results be any different without these parentheses?
In other words, how and why would the results change for the following variants?

        if (false) or (true)     % variant #1
        if (true)  or (false)    % variant #2
        if (true)  or (10< 9.9)  % variant #3
        if  true   or  10< 9.9   % variant #4
        if 10> 9.9 or  10< 9.9   % variant #5

Please post your thoughts in a comment below (expected results and the reason, for the main code snippet above and its variants), and then run the code. You might be surprised at the results, but not less importantly at the reasons. This deceivingly innocuous code snippet leads to interesting insight on Matlab's parser.
Full marks will go to the first person who posts the correct results and reasoning/interpretation of the variants above (hint: it's not as trivial as it might look at first glance).
Addendum April 9, 2019: I have now posted my solution/analysis of this puzzle here [3].

USA visit

I will be travelling in the US (Boston, New York, Baltimore) in May/June 2019. Please let me know (altmany at gmail) if you would like to schedule a meeting or onsite visit for consulting/training, or perhaps just to explore the possibility of my professional assistance to your Matlab programming needs.

Categories: Medium risk of breaking in future versions, Undocumented feature

Article printed from Undocumented Matlab: http://undocumentedmatlab.com

URL to article: http://undocumentedmatlab.com/articles/interesting-matlab-puzzle

URLs in this post:

[1] Andrew Janke: https://apjanke.net

[2] short-circuit evaluation: https://www.mathworks.com/help/matlab/ref/logicaloperatorsshortcircuit.html

[3] here: https://undocumentedmatlab.com/blog/interesting-matlab-puzzle-analysis

[4] Interesting Matlab puzzle – analysis : http://undocumentedmatlab.com/articles/interesting-matlab-puzzle-analysis

[5] An interesting uitree utility : http://undocumentedmatlab.com/articles/interesting-uitree-utility

[6] A couple of internal Matlab bugs and workarounds : http://undocumentedmatlab.com/articles/couple-of-bugs-and-workarounds

[7] Matlab DDE support : http://undocumentedmatlab.com/articles/matlab-dde-support

[8] Sliders in Matlab GUI : http://undocumentedmatlab.com/articles/sliders-in-matlab-gui

[9] Blurred Matlab figure window : http://undocumentedmatlab.com/articles/blurred-matlab-figure-window

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.