• Re: Style question: LOOP and multiple values

    From B. Pym@21:1/5 to Edi Weitz on Sun Sep 29 12:22:26 2024
    XPost: 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)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)