Page 1 of 1

Another option for FOR-ALL?

Posted: Fri May 07, 2010 3:41 am
by JeremyDunn
I would suggest generalizing FOR-ALL further to handle a list of test conditions as well as a single condition. There would also be an optional argument at the end where the user would define which logical operation is to be performed with all of the test conditions. The default for the optional argument would be the AND function. So

(for-all '(integer? zero?) '(0 0 0)) is the same as
(for-all '(integer? zero?) '(0 0 0) and)

this returns true if all members of the list satisfy all of the test conditions. If we wrote

(for-all '(integer? zero?) '(0 1 2) or)

this returns true if any of the test functions apply. Here is a sample implementation of the function:

Code: Select all

;; New FOR-ALL
(define (forall test lst (op and))
  (if-not (list? test)
     (for-all test lst)
     (apply op (map for-all test (dup lst (length test))))))