; File: bag.scm ; Title: bag-of. Works like map but does not collect #f. ; Functions with variable number of arguments ; see also bag-of defined in Teach Yourself Scheme ; Examples: ; (bag-of number? '(a 1 b 2 c 3)) -> (#t #t #t) ;; collects the numbers ; (bag-of (lambda(z) (if (number? z) z #f)) '(a 1 b 2 c 3)) -> (1 2 3) ;; (bag-of list? '((a) 2 ( b ) 4 )) -> (#t #t) ;; collects the negative numbers ; (bag-of (lambda(z) (if (< z 0) z #f)) '(1 0 -1 0 -2)) -> (-1 -2) (define-macro bag-of (lambda (f . L) `(remove-all #f (map ,f ,@L)) )) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; (remove-all 1 '(2 1 3 1 1 4 1)) -> (2 3 4) ; Our defintion of remove-all. ; This system has just remove, which removes first instance ; Ex: (remove 'a '( a b c a)) -> (b c a) (define (remove-all x L) (cond ((null? L) ()) ((equal? x (car L)) (remove-all x (cdr L))) (#t (cons (car L) (remove-all x (cdr L)))) )) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;