Sujet : Re: Style question: LOOP and multiple values
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lisp comp.lang.schemeDate : 29. Sep 2024, 13:22:26
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vdbgpq$1od96$1@dont-email.me>
User-Agent : XanaNews/1.18.1.6
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)