Skip to main content

count, count-if, count-if-not

count, count-if, count-if-not Function

Syntax:

count item sequence &key from-end start end key test test-not → n

count-if predicate sequence &key from-end start end key → n

count-if-not predicate sequence &key from-end start end key → n

Arguments and Values:

item—an object.

sequence—a proper sequence.

predicate—a designator for a function of one argument that returns a generalized boolean. from-end—a generalized boolean. The default is false.

test—a designator for a function of two arguments that returns a generalized boolean. test-not—a designator for a function of two arguments that returns a generalized boolean.

start, endbounding index designators of sequence. The defaults for start and end are 0 and nil, respectively.

key—a designator for a function of one argument, or nil.

n—a non-negative integer less than or equal to the length of sequence.

Description:

count, count-if, and count-if-not count and return the number of elements in the sequence bounded by start and end that satisfy the test.

The from-end has no direct effect on the result. However, if from-end is true, the elements of sequence will be supplied as arguments to the test, test-not, and key in reverse order, which may change the side-effects, if any, of those functions.

Examples:

(count #\a "how many A’s are there in here?")2 
(count-if-not #’oddp ’((1) (2) (3) (4)) :key #’car)2
(count-if #’upper-case-p "The Crying of Lot 49" :start 4)2

Exceptional Situations:

Should be prepared to signal an error of type type-error if sequence is not a proper sequence.

See Also:

Section 17.2 (Rules about Test Functions), Section 3.6 (Traversal Rules and Side Effects)

Notes:

The :test-not argument is deprecated.

The function count-if-not is deprecated.

Expanded Reference: count, count-if, count-if-not

Counting occurrences of an item

count returns the number of elements in the sequence that match the given item (using eql by default).

(count 'a '(a b a c a d))
=> 3
(count #\l "hello world")
=> 3
(count 1 #(1 2 1 3 1))
=> 3

Counting with a predicate using count-if

count-if counts elements that satisfy a predicate function.

(count-if #'evenp '(1 2 3 4 5 6))
=> 3
(count-if #'upper-case-p "Hello World")
=> 2
(count-if #'stringp '(1 "two" 3 "four"))
=> 2

Using a custom :test function

The :test keyword lets you specify a different comparison function.

(count "hello" '("Hello" "HELLO" "hello" "hi") :test #'string-equal)
=> 3

Using :key to extract a comparison value

The :key function is applied to each element before testing.

(count 'a '((a 1) (b 2) (a 3)) :key #'first)
=> 2

(count-if #'oddp '((1 a) (2 b) (3 c) (4 d)) :key #'car)
=> 2

Restricting the search range with :start and :end

(count #\a "banana" :start 1 :end 5)
=> 2

count-if-not (deprecated but functional)

count-if-not counts elements that do not satisfy the predicate. It is deprecated in favor of using complement.

(count-if-not #'oddp '(1 2 3 4 5))
=> 2
;; Preferred modern alternative:
(count-if (complement #'oddp) '(1 2 3 4 5))
=> 2