Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

Writing is turning one's worst moments into money. -- J.P. Donleavy


comp / comp.lang.lisp / Re: The LOOP macro

SubjectAuthor
o Re: The LOOP macroB. Pym

1
Subject: Re: The LOOP macro
From: B. Pym
Newsgroups: comp.lang.lisp, comp.lang.scheme
Organization: A noiseless patient Spider
Date: Sat, 31 Aug 2024 20:02 UTC
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Nobody447095@here-nor-there.org (B. Pym)
Newsgroups: comp.lang.lisp,comp.lang.scheme
Subject: Re: The LOOP macro
Date: Sat, 31 Aug 2024 20:02:56 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 44
Message-ID: <vavstc$14obt$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Sat, 31 Aug 2024 22:02:56 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="becbf8d70a038bf1db72383f6d2892a4";
logging-data="1204605"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX185pQ+6r9Il/X7/QM/2C2yO"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:M+IeeVJqogFCGZ7HrKfaEXa88os=
View all headers

> 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 '())]))

1

rocksolid light 0.9.8
clearnet tor