3

MATLAB operators usually translate into a function form as in the following examples:

  • ~A => not(A)
  • A + B => plus(A,B)
  • A(...) => subsref(...)
  • A(...) = ... => subsasgn(...)
  • etc.

Now please consider the operators && and ||.

The various documentation (1-doc for or, 2-doc for and, 3-the MATLAB Programming Fundamentals ebook), does not shed any light on this, and nor do help and, help or, help relop. This also didn't help: profile('on','-detail','builtin').

What I can say is that | seems to be redirected to or() judging by the following example:

>> 1 || [0,0]    
ans =    
     1

>> 1 | [0,0]    
ans =    
     1     1

>> or(1,[0,0])    
ans =    
     1     1

>> 1 && [0,0]
Operands to the || and && operators must be convertible to logical scalar values.

So my question is: assuming it's possible - how can one explicitly call the underlying function of && and ||?

(note: this question deals with the aspect of "how", not "why")

Dev-iL
  • 23,742
  • 7
  • 57
  • 99
  • 3
    I doubt that functional style call of AND could avoid executing the arguments. – knedlsepp Mar 11 '15 at 10:51
  • 2
    @Dev-iL: if you want a functional form of the [short-circuit operators](http://en.wikipedia.org/wiki/Short-circuit_evaluation), you'd have to use a language with a [lazy evaluation](http://en.wikipedia.org/wiki/Lazy_evaluation) model. – Amro Mar 11 '15 at 11:03
  • @Amro - You probably know that MATLAB provides some form of that (as demonstrated in Daniel's answer, for example). Nevertheless, I thank you for the insight. BTW - `&&` and `||` are the "living proof" that MATLAB does have lazy evaluation capabilities... The question is why can't we use said MATLAB capabilities for other things as easily? – Dev-iL Mar 11 '15 at 11:06
  • 1
    [This](http://ideone.com/JtTanj) and `sAND(@() expr1, @() expr2, ...)` is probably as close as it gets. – knedlsepp Mar 11 '15 at 11:41
  • @knedlsepp: I'm reminded of [this](http://www.mathworks.com/matlabcentral/fileexchange/39735-functional-programming-constructs/content//iif.m) which was presented a few years ago on Loren Shure's blog as part of a [series on functional programming](http://blogs.mathworks.com/loren/category/functional-programming/) techniques in MATLAB – Amro Mar 11 '15 at 11:48
  • @Amro: Yup, I read [Loren's post](http://blogs.mathworks.com/loren/2013/01/10/introduction-to-functional-programming-with-anonymous-functions-part-1/#c8d04efb-1a2d-4c35-afff-dd52e6c660d2) about inline `if`, when the OP asked [this](http://stackoverflow.com/questions/28797788) question. That's why it is quite similar. ;-) – knedlsepp Mar 11 '15 at 11:50

1 Answers1

2

There can't be a function implementing the underlying functionality. Assume there is a function scor which implements this operator, then calling scor(true,B) would evaluate B before calling scor, but the operator does not evaluate B.

Obviously scor could be defined scor=@(x,y)(x||y), but it will evaluate B in the upper case.

/Regarding the comment using function handles, this might be a workaround:

%not printing a:
true||fprintf('a')
%printing a:
scor=@(x,y)(x||y)
scor(true,fprintf('a'))
%not printing a:
scor(true,@()(fprintf('a')))
Daniel
  • 36,610
  • 3
  • 36
  • 69
  • Yes, but why not something like `builtin('&&',a,b)`? Would this also evaluate the inputs right away? – Dev-iL Mar 11 '15 at 10:52
  • @Dev-iL: Again, `builtin` is a function, b is evaluated. – Daniel Mar 11 '15 at 10:53
  • 1
    @Dev-iL: There is no functional form of the logical short-circuiting operators `&&` and `||`, and for good reason... The arguments would have to be evaluated before calling such functions, which defeats the purpose of [short-circuiting behavior](http://www.mathworks.com/help/matlab/ref/logicaloperatorsshortcircuit.html#bt_0nai-1). – Amro Mar 11 '15 at 10:54
  • 1
    Perhaps `builtin('&&',a,@()b)` ? – Dev-iL Mar 11 '15 at 10:54
  • 1
    @Dev-iL: It seems when you were installing Matlab you were hoping to get Haskell. ;-) – knedlsepp Mar 11 '15 at 11:02
  • But that's the thing.... we're dealing with workarounds here... not the actual calls to `||` and `&&`, however they may be realized.... I tend to agree with @Amro 's point. @knedlsepp - I'm just exploring... :P – Dev-iL Mar 11 '15 at 11:02
  • 2
    btw - just noticed that SO suggests the following [related question on a similar subject](http://stackoverflow.com/questions/25913237/is-there-actually-a-reason-why-overloaded-and-dont-short-circuit) which I find very informative. – Dev-iL Mar 11 '15 at 11:24
  • 1
    (I'm accepting this answer under the assumption that the distinguished Daniel and @Amro don't fit [Clarke's 1st law](http://en.wikipedia.org/wiki/Clarke%27s_three_laws#Clarke.27s_first_law) :) ) – Dev-iL Mar 11 '15 at 11:34
  • @Dev-iL Well, Daniel's reasoning in his first paragraph _seems_ to be irrefutable! :-) – Luis Mendo Mar 11 '15 at 12:53