4

What boolean/logical convenience functions / macros are available for Emacs Lisp in either the core set of functions or external packages?

I'm interested in just about anything other than the built-in logical operators in and, or, not (null) and conditional forms (if, when, unless, cond). To illustrate the sort of convenience functions I have in mind:

Common Lisp has sequence predicates such as some and every and other functions like count-if. When used with an identity function, (some identity my-list) it performs a logical OR over elements in the list and (every identity my-list) performs a logical AND over elements in the list. These are convenient and are nearly ideal, but aren't purely logical functions because they require a predicate that is applied to each element. These functions are available in cl-lib.

and and or conceptually fill the same niche, but clearly note that their use is limited because they are implemented as macros. Expressions such as (apply and my-list) are not possible.

I have reviewed the list of ~1089 built-in Elisp functions and macros and only know of two relevant convenience functions. booleanp checks if the argument's value is t or nil and not is an alias for null.

There are also numerous boolean vector convenience functions that are a part of Elisp, but as far as I can tell are only suitable for binary manipulation.

@Dan points out the use of memq to search for nil values in a list can evaluate functions of logical values of a list using variations of (memq nil my-list).

An example of a potential convenience function would be a boolean identity function that simply converts a value to its canonical boolean form.

(defun boolean-identity (x)
  (if x t nil))
  • 3
    Unclear; please try to specify what you want more clearly. What does "functions for processing boolean values" mean? In Lisp, every value is a Boolean value (every value is nil or non-nil). Processing how, to do what? Just what kind of values are in your "list of boolean values"? – Drew Dec 22 '15 at 21:04
  • That would be probably (if x t nil). (x) would treat x as a function and call it. I've only heard about purity in logic in the context of Prolog language, where there are logical and extra-/non- logical predicates. Is this what you mean when you say "purely logical functions"? – wvxvw Dec 23 '15 at 9:41
  • @wvxvw Nope; that was a typo :-\ – ebpa Dec 23 '15 at 14:41
6

It's not 100% clear what you're looking to do. As a general response, Emacs Lisp treats all non-nil values as true for logical testing purposes.

As such, just about the only "boolean" predicates, in the sense that you seem to be using the term, are null and not, which, as the manual states, are identical:

This function returns t if object is nil, and returns nil otherwise. This function is identical to not, but as a matter of clarity we use null when object is considered a list and not when it is considered a truth value.

So:

(null nil)                              ; => t
(not nil)                               ; => t
(null t)                                ; => nil
(not t)                                 ; => nil
(null '(a b c))                         ; => nil
(null '())                              ; => t (empty list is nil)

If you're looking to see if some or all of the elements of a list are non-nil, you can use memq. Here's an example of testing that each element is non-nil:

(memq nil '(a b c))                     ; => nil
(null (memq nil '(a b c)))              ; => t (so all elements are non-nil)

Here's an example showing that not all elements are non-nil:

(memq nil '(a b nil))                   ; => (nil)
(null (memq nil '(a b nil)))            ; => nil (so not all elements are non-nil)

Note that you can also use or and and to do implicit nil testing:

(and 1 2 3)                             ; => 3
(and 1 2 nil)                           ; => nil
(or 1 2 3)                              ; => 1
(or nil 2 3)                            ; => 2
(or nil nil nil)                        ; => nil
4

Based on the example code you provided, I am guessing you are looking for this:

(-non-nil my-list-of-boolean-values)

Of course -non-nil is from the dash library. If you don't want to use dash, then you might have to go with:

(cl-mapcan (lambda (elt) (and elt (list elt)))
           my-list-of-boolean-values)
  • I'm glad you made this observation. I had not been familiar with dash as a library of utility functions. I presumed that it was an internal utility function of what I now what I now realize is the completely distinct package helm-dash (for the Dash documentation project). Dash.el has a wealth of useful functions! – ebpa Dec 23 '15 at 7:04
2

An alternative to @tarsius's solution:

(cl-remove-if-not #'identity (list 1 2 3 nil)) => '(1 2 3)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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