Sysop: | Amessyroom |
---|---|
Location: | Fayetteville, NC |
Users: | 43 |
Nodes: | 6 (0 / 6) |
Uptime: | 98:10:17 |
Calls: | 290 |
Files: | 905 |
Messages: | 76,483 |
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))
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))
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))
Gauche Scheme
(let1 y 0 (collect-till x (random 10) (> (inc! y x) 50)))
===>
(7 9 4 0 6 4 1 3 2 7 0 7 0 1)
Given:
(use srfi-27) ;; random-integer
(define random random-integer)
(define-syntax collect-till
(syntax-rules ()
[(collect-till v expr0 expr1)
(let ((res '()))
(let go ((v expr0))
(set! res (cons v res))
(if expr1 (reverse res) (go expr0))))]))
(collect-till x (random 10) (even? x))
===>
(7 1 6)