Jakob Ƙstergaard Hegelund

Tech stuff of all kinds

Useful return from short-circuit or


This could have been a bootnote on the next post but time flew and now it will have to be a post on its own. Consider two solutions to the same problem:

// C++ // a(), b() and c() return std::optional std::string pick() { return a().value_or(b().value_or(c().value_or("-default-"))); } ;; LISP ;; (a), (b) and (c) return nil or string (defun pick () (or (a) (b) (c) "-default-"))

My point earlier was that having or return not a boolean, but the actual first non-false argument led to some simple beautiful constructs. I stand by that, but there's another important difference between the two solutions. Consider first the actual sequence of evaluations in the C++ example:

// First, as evaluate the arguments to the value_or functions, we // must call all three functions tmp0 = a(); tmp1 = b(); tmp2 = c(); tmp3 = std::string("-default-"); // Now, with the arguments evaluated, we can "fold" back to get // the result tmp4 = tmp2.value_or(tmp3); tmp5 = tmp1.value_or(tmp4); tmp6 = tmp0.value_or(tmp5); // Our result is in tmp6

Since value_or is a function, it's argument must be evaluated before the function can be called. With this follows that it is necessary to evaluate all three functions before we can decide which result to use - even though all we want is the first value. Compare that to a short-circuit or:

(let ((tmp0 (a))) (if tmp0 tmp0 (let ((tmp1 (b))) (if tmp1 tmp1 (let ((tmp2 (c))) (if tmp2 tmp2 "-default-"))))))

The important point here is, that if (a) evaluates to non-nil, it is returned and nothing else is evaluated. Only if it is nil, do we evaluate (b), and so forth.

So in conclusion; not only is a short-circuit or operator that actually returns the first non-false value useful for elegant programming constructs, it is also the most efficient solution for the solution of this simple problem.

In programming, if you find that your solution to a simple problem is not simple, you are in trouble. The short-circuit or is indeed a simple solution to a simple problem.