• Re: Style question: LOOP and multiple values

    From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp,comp.lang.scheme on Sun Sep 29 12:22:26 2024
    From Newsgroup: comp.lang.scheme

    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)
    --- Synchronet 3.22a-Linux NewsLink 1.2