Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

Excellent day to have a rotten day.


comp / comp.lang.lisp / Re: simple loop question

SubjectAuthor
* Re: simple loop questionB. Pym
`- Re: simple loop questionB. Pym

1
Subject: Re: simple loop question
From: B. Pym
Newsgroups: comp.lang.lisp, comp.lang.scheme
Organization: A noiseless patient Spider
Date: Tue, 3 Sep 2024 07:29 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: simple loop question
Date: Tue, 3 Sep 2024 07:29:30 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 31
Message-ID: <vb6dso$38rv7$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Tue, 03 Sep 2024 09:29:30 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="e3a74a1d764f3cc8dc1558bbaff4093f";
logging-data="3436519"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX186CZtM4lfHljSirankWEZX"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:K74mPTlwt2iCc+AhYa4TVy/kgZU=
View all headers

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)

Subject: Re: simple loop question
From: B. Pym
Newsgroups: comp.lang.lisp, comp.lang.scheme
Organization: A noiseless patient Spider
Date: Wed, 4 Sep 2024 00:47 UTC
References: 1
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: simple loop question
Date: Wed, 4 Sep 2024 00:47:21 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 52
Message-ID: <vb8aml$3hjes$1@dont-email.me>
References: <vb6dso$38rv7$1@dont-email.me>
Injection-Date: Wed, 04 Sep 2024 02:47:22 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="3f696a5740fefc93e18375f66e038165";
logging-data="3722716"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19704mJoSY4VNgtVBC6kZ7M"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:C4EBde7TWSqBuVNrDNpPPMm5wBA=
View all headers

B. Pym 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))
>
> 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)

Revision:

(collect-till (x (y 0)) (random 10) x (> (inc! y x) 50))

Given:

(define-syntax collect-till
(syntax-rules ()
[(collect-till (v (w x) ...) v-val collect test)
(let ((res '()) (w x) ...)
(let go ((v v-val))
(set! res (cons collect res))
(if test (reverse res) (go v-val))))]
[(collect-till v v-val collect test)
(collect-till (v) v-val collect test)]))

1

rocksolid light 0.9.8
clearnet tor