You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
823 B
Racket
32 lines
823 B
Racket
#lang racket/base
|
|
(require racket/list racket/bool)
|
|
|
|
(provide (all-defined-out))
|
|
|
|
(module+ test (require rackunit))
|
|
|
|
|
|
|
|
(define (count_if pred xs)
|
|
;; Count the number of elements of seq for which the predicate is true.
|
|
(length (filter-not false? (map pred xs))))
|
|
|
|
(module+ test
|
|
(check-equal? (count_if procedure? (list 42 null max min)) 2))
|
|
|
|
(define (find_if pred xs)
|
|
;; If there is an element of seq that satisfies predicate; return it.
|
|
(or (findf pred xs) null))
|
|
|
|
(module+ test
|
|
(check-equal? (find_if procedure? (list 3 min max)) min)
|
|
(check-equal? (find_if procedure? (list 1 2 3)) null))
|
|
|
|
|
|
(define (every pred xs)
|
|
;;;True if every element of seq satisfies predicate.
|
|
(andmap pred xs))
|
|
|
|
(module+ test
|
|
(check-true (every procedure? (list min max)))
|
|
(check-false (every procedure? (list min 3)))) |