Sujet : Re: the "loop" macro
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lispDate : 30. Jun 2025, 04:22:58
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <103svuh$1v98h$1@dont-email.me>
References : 1 2 3
User-Agent : XanaNews/1.18.1.6
B. Pym wrote:
B. Pym wrote:
B. Pym wrote:
(loop for item in list
maximize (f item))
It's shorter in Gauche Scheme.
(use srfi-42) ;; max-ec
(max-ec (: x '(-8 2 0)) (abs x))
===>
8
(loop for x in '(1 2 3 4 5 6 7)
when (evenp x)
collect x into evens
else
collect x into odds
finally
(return (values evens odds)))
=> (2 4 6), (1 3 5 7)
It's shorter in Gauche Scheme.
(partition even? '(2 3 4 5 6 7))
===>
(2 4 6)
(3 5 7)
Using collectors.
(let ((odds (mlistbag)) (evens (mlistbag)))
(dolist (x '(1 2 3 4 5 6 7))
(if (odd? x) (odds x) (evens x)))
(values (evens) (odds)))
===>
(2 4 6)
(1 3 5 7)
Given:
(define (mbag init func :optional (pass-through #f))
(let ((val init) (func func) (pass-through pass-through))
(lambda args
(if (null? args)
val
(begin
(set! val
;; A "kons" may have been supplied.
((if (null? (cdr args)) func (cadr args))
(car args) val))
(if pass-through
(car args)
val))))))
(define (mlistbag :optional (pass-through #t))
(let ((bag (mbag '() cons pass-through)))
(lambda args
(if (null? args)
(reverse (bag))
(apply bag args)))))