Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

.Re: simple loop question

7 views
Skip to first unread message

Robert L.

unread,
Jul 13, 2017, 3:45:17 AM7/13/17
to
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))

(use miscmacros) ; inc!
(use srfi-121) ; generators
(use holes) ; ##

(generator->list
(let ((s 0))
(gtake-while ##(and (<= s 50) (inc! s !!)) ##(random 10))))

===>
(1 4 1 4 9 3 8 5 3 5 6 1 9)


--
I wrote until my fingers were blue, over and over: "Political
Correctness is a religion." I proved it and showed you why it is so
important that Political Correctness is a religion.
--- Bob Whitaker

Robert L.

unread,
Jul 22, 2017, 4:54:04 PM7/22/17
to
On 7/13/2017, Robert L. wrote:

> 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))
>
> (use miscmacros) ; inc!
> (use srfi-121) ; generators
> (use holes) ; ##
>
> (generator->list
> (let ((s 0))
> (gtake-while ##(and (<= s 50) (inc! s !!)) ##(random 10))))
>
> ===>
> (1 4 1 4 9 3 8 5 3 5 6 1 9)

(use foof-loop)

(define (go)
(loop ((let x (random 10))
(y 0 (+ y x))
(until (> y 50))
(for r (listing x)))
=> r))

#;4> (go)
(2 9 0 4 9 9 3 0 9 0 9)
#;5> (go)
(2 2 3 0 2 5 8 7 2 8 4 9)
#;6> (apply + '(2 2 3 0 2 5 8 7 2 8 4 9))
52


(define (test)
(define r (go))
(if (and (> (apply + r) 50)
(<= (apply + (butlast r)) 50))
(display ".")
(print "bad result: " r)))

(use miscmacros) ; repeat

(repeat 70 (test))
===>
......................................................................

--
Amazon bans book after it received 300 5-star reviews.
http://www.tomatobubble.com/worldwarii.html
0 new messages