Recipes


Recipes


 

Functional Programming-related

 

om-filter func lst

Works just like filter where the returned list consists of elements that failed to pass the test func. Additionally returns a list containing the elements tossed out.

(define (filter-keep func lst)
  (let ((good '()) (bad '()))
    (map (lambda (x)
           (if (func x)
               (set! bad (cons x bad))
               (set! good (cons x good))))
         lst)
    (values (reverse good) (reverse bad))))

This version returns a list instead of multiple values. The filtered out list is the car of the returned list, and the elements tossed out are in the cadr of it.

(define (filter-keepl func lst)
  (let ((good '()) (bad '()))
    (map (lambda (x)
           (if (func x)
               (set! bad (cons x bad))
               (set! good (cons x good))))
         lst)
    (list (reverse good) (reverse bad))))