1

Is there a convenient alternative of and() in Matlab that does not check existence, number of input or output arugments, and returns a false as soon as the first false is found without evaluating the expressions in subsequent inputs?

For example, I would like

and(0,a),
and(0,error()),

to both return false as opposed to returning error messages. Once an earliest input argument returns false, I have no use of subsequent input arguments and I am happy to ignore syntax errors. But Matlab isn't.

(The more likely scenario for me is that the false case of preceding inputs cover any syntax errors in later inputs.)

Is there a way around this? If I write an alternative of and() with a (Matlab) loop on varargin, will the alternative be slower?

Argyll
  • 8,591
  • 4
  • 25
  • 46

2 Answers2

0

Using the && operator solves your problem,

0 && a

0 && error()

will return

ans =

  logical

   0

Of course, even when a is undefined.

Caveat: and() can take (syntax checked) array arguments while && cannot. The different answers and comments in this question explain in more details.

Argyll
  • 8,591
  • 4
  • 25
  • 46
myradio
  • 1,703
  • 1
  • 15
  • 25
  • Is there any difference between `and()` and `&&`? And is there any restriction on the use of `&&` syntax wise? – Argyll Aug 03 '19 at 07:22
  • `and()` is the same as `&`, this is, the logical operator. `&&` is instead a logical and operator with short-circuiting, which means that is does exactly what you want. It only executes until the first false is found. There is no syntax restriction compared to `&`. – myradio Aug 03 '19 at 07:26
0

The MATLAB interpreter (just like any other interpreter I’ve come across) parses all the input arguments to a function before calling the function. The function is passed the results of evaluating the arguments. Therefore, it is not possible to have a function control which of its input arguments are parsed.

There is no functional equivalent to &&, the short-circuit logical AND. The function and is equivalent to & and does not short-circuit.

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
  • Why does the interpreter parse all arguments by necessity? – Argyll Aug 03 '19 at 13:28
  • @Argyll: OK, so maybe not strictly necessary, I guess. But delaying evaluation would certainly lead to a more complex interpreter, and delayed and confusing error messages. – Cris Luengo Aug 03 '19 at 13:36
  • Is error message the reason towards necessity then? Why would it be more complex if it does less checks? – Argyll Aug 03 '19 at 13:39
  • @Argyll: There is also the expectation of execution order: if functions have side-effects, for example print something to the screen, in what order do you expect these functions to do their thing? `fun(a(), b())`. – Cris Luengo Aug 03 '19 at 13:44
  • evaluate functions as nested expressions in the order of the expressions? start with first line of `fun` until `a` is required; when `a` is required, evaluate `a()`, which may potentially include `fun` as well. Isn't that natural of a functional language? – Argyll Aug 03 '19 at 13:47
  • So depending on `fun` either `a` or `b` would be called first? I’m not sure I would appreciate that from a language. Also note that in a functional programming language, functions don’t have side effects. MATLAB is not a functional programming language. – Cris Luengo Aug 03 '19 at 13:52
  • I am not sure if you necessarily need functions to be unable to modify variables that outlive the function in a functional language. And yes, depending on `fun`, which also means depending on the context in which `fun` is called, either `a` or `b` may be called first or not at all. Otherwise, it's restrictive, isn't it? The need for short-circuiting in logical operators is an example. – Argyll Aug 03 '19 at 13:56
  • Rather, whenever the order of evaluation is reversed by some special rule, instead of the order of expressions themselves, it is an extra special rule to have to memorize. But yes, I can see why type checking is at the beginning. It is probably easier to implement. But it shouldn't be more efficient. When an expression throws error, input type can be checked then. It's not necessarily less efficient. – Argyll Aug 03 '19 at 14:00
  • The definition of functional programming languages is the elimination of side effects. There is no state. Regarding evaluation order: you’ll find all imperative languages evaluate the arguments before evaluating the function itself. MATLAB, Python, C, C++, Java, ... I don’t know any languages that don’t do this, so for me it is the natural thing to do. – Cris Luengo Aug 03 '19 at 14:08
  • Scheme. . . . (hit char count) – Argyll Aug 03 '19 at 14:19