Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

You will be held hostage by a radical group.


comp / comp.lang.lisp / Re: Style question: LOOP and multiple values

SubjectAuthor
o Re: Style question: LOOP and multiple valuesB. Pym

1
Subject: Re: Style question: LOOP and multiple values
From: B. Pym
Newsgroups: comp.lang.lisp, comp.lang.scheme
Organization: A noiseless patient Spider
Date: Sun, 29 Sep 2024 12:22 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: Style question: LOOP and multiple values
Date: Sun, 29 Sep 2024 12:22:26 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 34
Message-ID: <vdbgpq$1od96$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Sun, 29 Sep 2024 14:22:26 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="ead6ae14c337576b56021eac81531934";
logging-data="1848614"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/pYiCWRJbG/bWTBM+V2LMo"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:XNd8GnGsXBtv4yvzGckfjlLYxuM=
View all headers

Edi Weitz wrote:

> I wanted to write a function that reads whitespace-separated numbers
> from a string, i.e. given a string like "1 2 3 4" I wanted my function
> to return the list (1 2 3 4).
>
> Two different solutions came to mind:
>
> (defun foo1 (string)
> (loop for start = 0 then pos
> for (object pos) = (multiple-value-list
> (read-from-string string nil nil :start start))
> while object
> collect object))
>
> (defun foo2 (string)
> (let ((start 0) object result)
> (loop
> (multiple-value-setq (object start)
> (read-from-string string nil nil :start start))
> (unless object
> (return-from foo2 (nreverse result)))
> (push object result))))

Scheme

(define (read-em)
(define x (read))
(if (eof-object? x) () (cons x (read-em))))

(with-input-from-string "2 3 4 5" read-em)

===>
(2 3 4 5)

1

rocksolid light 0.9.8
clearnet tor