Sujet : Re: The LOOP macro
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lisp comp.lang.schemeDate : 31. Aug 2024, 22:02:56
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vavstc$14obt$1@dont-email.me>
User-Agent : XanaNews/1.18.1.6
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 '())]))