Another option for FOR-ALL?

Q&A's, tips, howto's
Post Reply
JeremyDunn
Posts: 11
Joined: Wed Apr 07, 2010 12:18 am

Another option for FOR-ALL?

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

Post Reply