Search code examples
elispspecial-form

How to use “and” with “seq-reduce” in Elisp?


I’m trying to write something like this:

(setq l '(nil t nil nil))
(seq-reduce 'and l t)

I get this error:

Invalid function: and

My understanding, after a bit of googling, is that this is due to and being a special form. What would be a good way to make this work? Is there a function equivalent to and? Is there some way to make a function out of a special form? I couldn’t find any documentation about this. I tried to write a function that does what and does, but that seems overkill.


Solution

  • The seq-reduce function uses funcall to invoke its function argument, and as shown in the funcall documentation, a special form like and can't be passed to it; calling (funcall 'and t nil) results in an Invalid function: #<subr and> error.

    You can make the call to and in a lambda instead:

    (let ((l '(nil t nil nil)))
      (seq-reduce (lambda (acc v) (and acc v)) l t))