Skip to content

LISP - notevery function

The notevery function is a sequence predicate that tests if at least one element in a sequence fails to satisfy a given test. It is the logical negation of the every function.

Think of it this way: (every predicate sequence) asks, “Does every element satisfy the predicate?” while (notevery predicate sequence) asks, “Is there at least one element that does not satisfy the predicate?”.

Logically, (notevery p s) is equivalent to (not (every p s)).

(notevery predicate &rest sequences+)
  • predicate: A function that accepts as many arguments as there are sequences and returns a boolean value (t or nil).
  • sequences+: One or more sequences (lists, vectors, or strings) to be tested.

It returns t if the predicate returns nil for at least one element (or set of elements). It returns nil only if the predicate returns t for all elements. The function short-circuits, meaning it stops and returns t as soon as it finds the first element that fails the test.

Example: Checking if a List Contains an Odd Number

Section titled “Example: Checking if a List Contains an Odd Number”

Here, notevery with the predicate #'evenp checks if there is any number in the sequence that is not even.

;;; Test a list of only even numbers
(format t "Is there an odd number in '(2 4 6 8)? ~a~%"
(notevery #'evenp '(2 4 6 8))) ; -> NIL, because all are even.
;; Test a vector of only even numbers
(format t "Is there an odd number in '#(2 4 6 8)? ~a~%"
(notevery #'evenp '#(2 4 6 8))) ; -> NIL, because all are even.
;; Test a list containing an odd number
(format t "Is there an odd number in '(2 4 9 8)? ~a~%"
(notevery #'evenp '(2 4 9 8))) ; -> T, because 9 is not even.
Is there an odd number in '(2 4 6 8)? NIL
Is there an odd number in '#(2 4 6 8)? NIL
Is there an odd number in '(2 4 9 8)? T

You can use a lambda function for more complex, custom predicates. Let’s check if any number in a list is 10 or less.

(let ((predicate #'(lambda (x) (> x 10))))
;; Test a list where all numbers are > 10
(format t "Is any number in '(11 15 14) not > 10? ~a~%"
(notevery predicate '(11 15 14))) ; -> NIL
;; Test a list where some numbers are not > 10
(format t "Is any number in '(11 5 14) not > 10? ~a~%"
(notevery predicate '(11 5 14)))); -> T
Is any number in '(11 15 14) not > 10? NIL
Is any number in '(11 5 14) not > 10? T

Example: Checking a String for Non-Alphabetic Characters

Section titled “Example: Checking a String for Non-Alphabetic Characters”

notevery works on strings as well. We can check if a string contains any non-alphabetic characters.

;; Check a purely alphabetic string
(format t "Does 'tutorialspoint' contain any non-alphabetic characters? ~a~%"
(notevery #'alpha-char-p "tutorialspoint")) ; -> NIL
;; Check a string with digits
(format t "Does 'lisp-101' contain any non-alphabetic characters? ~a~%"
(notevery #'alpha-char-p "lisp-101")) ; -> T (stops at '-')
Does 'tutorialspoint' contain any non-alphabetic characters? NIL
Does 'lisp-101' contain any non-alphabetic characters? T

When given multiple sequences, the predicate receives one element from each sequence at each step. notevery will test if the predicate is ever false for any corresponding element pair.

;; Check if any element in the first list is NOT less than the corresponding element in the second.
(format t "Is (notevery #'< '(1 2 3) '(4 5 6)) -> ~a~%"
(notevery #'< '(1 2 3) '(4 5 6))) ; -> NIL, because 1<4, 2<5, and 3<6 are all true.
;; Check again, but with one failing case.
(format t "Is (notevery #'< '(1 8 3) '(4 5 6)) -> ~a~%"
(notevery #'< '(1 8 3) '(4 5 6))) ; -> T, because (not (< 8 5)) is true.
Is (notevery #'< '(1 2 3) '(4 5 6)) -> NIL
Is (notevery #'< '(1 8 3) '(4 5 6)) -> T

notevery is particularly useful for validation and checks where you are looking for a single failure or invalid item.

  • Data Validation: To check if any submitted form field is invalid. (notevery #'valid-field-p form-data) would return T if there’s at least one bad field.
  • Security: To confirm that a password contains at least one non-alphanumeric character. (notevery #'alphanumericp "p@ssword1") returns T, confirming the password meets the complexity rule.
  • Quality Control: To find if any item in a production batch fails a quality check. (notevery #'quality-passed-p batch-items) returns T if a defect is found.