From Newsgroup: comp.lang.scheme
Or if the predicate functions FOO-P and BAR-P are sufficiently
expensive that you don't want to compute more often than absolutely necessary:
(loop for x in things
for foo-p = (foo-p x)
for bar-p = (bar-p x)
when foo-p collect x into foos
when bar-p collect x into bars
when (and foo-p bar-p) collect x into both
finally (return (values foos bars both)))
Gauche Scheme
(define things '(2 9 33 -44 0 5 -27 88 6 99 -7))
(rlet1 al '()
(dolist (x things)
(let ((odd-p (odd? x)) (neg-p (negative? x)))
(if odd-p (apush! al 'odd x))
(if neg-p (apush! al 'neg x))
(and odd-p neg-p (apush! al 'both x)))))
((both -7 -27) (neg -7 -27 -44) (odd -7 99 -27 5 33 9))
Given:
(define-syntax ainc!
(syntax-rules ()
[(_ alist key val func default)
(let ((pair (assoc key alist)))
(if pair
(set-cdr! pair (func val (cdr pair)))
(set! alist (cons (cons key (func val default)) alist))))]
[(_ alist key val func)
(ainc! alist key val func 0)]
[(_ alist key val)
(ainc! alist key val +)]
[(_ alist key)
(ainc! alist key 1)]))
(define-syntax apush!
(syntax-rules ()
[(_ alist key val) (ainc! alist key val cons '())]))
--- Synchronet 3.22a-Linux NewsLink 1.2