Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You only mention the case where there are exactly 2 expressions to evaluate. What happens if there are 5?</p> <pre><code>;; returns first true value, evaluating only as many as needed (or (f x) (g x) (h x) (i x) (j x)) </code></pre> <p>Would you nest if-statements? I'm not sure how I'd do this in Python. It's <em>almost</em> like this:</p> <pre><code>any(c(x) for c in [f, g, h, i, j]) </code></pre> <p>except Python's <code>any</code> throws away the value and just returns <code>True</code>. (There might be a way to do it with <code>itertools.dropwhile</code>, but it seems a little awkward to me. Or maybe I'm just missing the obvious way.)</p> <p>(As an aside: I find that Lisp's builtins don't <em>quite</em> correspond to what their names are in other languages, which can be confusing. Lisp's <code>IF</code> is like C's ternary operator <code>?:</code> or Python's conditional expressions, for example, not their if-statements. Likewise, Lisp's <code>OR</code> is in some ways more like (but not exactly like) Python's <code>any()</code>, which only takes 2 expressions. Since the normal <code>IF</code> returns a value already, there's no point in having a separate kind of "if" that can't be used like this, or a separate kind of "or" that only takes two values. It's already as flexible as the less common variant in other languages.)</p> <p>I happen to be writing code like this right now, coincidentally, where some of the functions are "go ask some server for an answer", and I want to stop as soon as I get a positive response. I'd never use <code>OR</code> where I really want to say <code>IF</code>, but I'd rather say:</p> <pre><code>(setq did-we-pass (or (try-this x) (try-that x) (try-some-other-thing x) (heck-maybe-this-will-work x)) </code></pre> <p>than make a big tree of IFs. Does that qualify as "flow control" or "functional"? I guess it depends on your definitions.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload