• Re: Read-from-string

    From B. Pym@21:1/5 to Pascal J. Bourguignon on Tue Jul 16 13:24:22 2024
    Pascal J. Bourguignon wrote:

    words, but in reality I'll be reading from an input where the number
    of words is unknown to me. Is there a way to circumvent this "repeat
    3", because it would be "repeat n" and n is unknwon (it's as many
    words that the string contains).


    (with-input-from-string (s "lala tata bobo dada qwerty moo goo")
    (loop for token = (read s nil nil nil)
    while token
    collect token))


    (let ((data "lala tata bobo dada nil qwerty moo goo"))
    (with-input-from-string (s data)
    (loop for token = (read s nil nil nil)
    while token
    collect token)))
    (LALA TATA BOBO DADA)

    Gauche Scheme

    (use file.util)
    (file->sexp-list "output.dat")


    Another way:

    (use gauche.generator)

    (let ((data "lala tata bobo dada #f qwerty moo goo"))
    (with-input-from-string data
    (lambda() (generator->list read))))

    ===>
    (lala tata bobo dada #f qwerty moo goo)


    Another way:

    (let ((data "lala tata bobo dada #f qwerty moo goo"))
    (with-input-from-string data
    (lambda() (collect-while list read))))

    ===>
    (lala tata bobo dada #f qwerty moo goo)

    Given:

    (define (collect-while pred gen . opt-key)
    (let ((key (if (pair? opt-key) (car opt-key) values)))
    (do ((x (gen) (gen))
    (res '() (cons (key x) res)))
    ((or (eof-object? x) (not (pred x))) (reverse res)))))

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