any-none-all-foo.scm added by gahr on Wed Jun 19 20:46:28 2024

(import (chicken base))

(define (check pred lst when-null when-found reverse?)
  (let ((mod (if reverse? not identity)))
    (let loop ((lst lst))
      (cond ((null? lst) when-null)
            ((mod (pred (car lst))) when-found)
            (else (loop (cdr lst)))))))

(define (any pred lst)  (check pred lst #f #t #f))
(define (none pred lst) (check pred lst #t #f #f))
(define (all pred lst)  (check pred lst #t #f #t))
(define (foo pred lst)  (check pred lst #f #t #t))

(define lt5 (cut < <> 5))
(define gt5 (cut > <> 5))

(print (any  lt5 '(1 2 3 4)))
(print (any  gt5 '(1 2 3 4)))
(print (none lt5 '(1 2 3 4)))
(print (none gt5 '(1 2 3 4)))
(print (all  lt5 '(1 2 3 4)))
(print (all  gt5 '(1 2 3 4)))
(print (foo  lt5 '(1 2 3 4)))
(print (foo  gt5 '(1 2 3 4)))