Another option for FOR-ALL?

Started by JeremyDunn, May 06, 2010, 08:41:48 PM

Previous topic - Next topic

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:

;; 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))))))