Test if all items in list pass the predicate

Problem

I have a list and I want to check if function return true for all items.

Solution

(define (every? fn list)
  (or (null? list)
      (and (fn (car list)) (every? fn (cdr list)))))

Credit: Jakub T. Jankiewicz

SRFI

SRFI-1 has function every, but every? always return #f or #f that's why it has question mark.

Usage

(every? number? '(1 2 3 4))
;; ==> #t
(every? number? '(1 foo 3 4))
;; ==> #f
(every? number? '())
;; ==> #t
(every? string->number '("foo" "bar" "baz"))
;; ==> #f

Back to the Scheme Cookbook