Sujet : Re: simple loop question
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lisp comp.lang.schemeDate : 22. Sep 2024, 07:45:50
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vcoeet$249pf$1@dont-email.me>
User-Agent : XanaNews/1.18.1.6
Lars Brinkhoff wrote:
use LOOP to collect random integers into a list until the sum of that
list exceeds a constant (say 50).
(loop for x = (random 10) collect x sum x into y until (> y 50))
Let's see if it's shorter in Gauche Scheme.
(loop (y) (:coll (++. y (random 10))) (:till (> y 50)))
Yes, it is.
Given:
(use srfi-27) ;; random-integer
(define random random-integer)
;; Returns the added number, not the sum.
(define-syntax ++.
(syntax-rules ()
[(++. place val)
(let ((x val)) (set! place (+ x place)) x)]
[(++. place) (++. place 1)]))
I don't want to include the rather lengthy source for
"loop" here, so defining it is left as an exercise for
the reader.