4

I would like to evaluate a list of booleans like

(nil nil t)

into a single boolean, such that if any element is true, then the expression evaluates to true.

I first looked at the (or) function in elisp, but it does not take a list of booleans as an argument. Instead it requires its arguments to be "un-wrapped" outside of a list. In other words,

(or (nil nil t))

simply evaluates to the entire list (nil nil t) because it treats the it as a single argument. I would like something that instead evaluates exactly to t. - I understand that (nil nil t) is truthy, but that's beside the point.

How can I convert (or (nil nil t)) into just (or nil nil t) or just t?

After some thought, I can think of

(eval (cons 'or '(nil nil t)))

but is there some other more idiomatic way that I'm missing?

1

2 Answers 2

3

If you wanted this for a function you would use apply, but as or is a special form you can't do that. In particular, or only evaluates as many arguments as it needs to.

You could write a macro:

(defmacro or-list (list)
  `(or ,@(eval list)))

(setq mylist '(a b c))

(or-list mylist) then expands to (or a b c)

1
2

How about this?

(defun or-list (list)
  (cl-some #'identity list))

Here cl-some takes two arguments: a predicate and a list and returns non-nil if the predicate applied to some member of the list is non-nil. In our case, the identity function is a good predicate since we only want to test whether our list members are themselves non-nil.

2
  • Good. But it might help to elaborate a bit, explaining how the code does what's requested.
    – Drew
    Jan 12, 2021 at 16:44
  • @Drew: done! (Though I think the code is pretty self-explanatory.) Jan 12, 2021 at 23:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.