Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: "B. Pym" Newsgroups: comp.lang.lisp Subject: Re: Read-from-string Date: Tue, 16 Jul 2024 13:24:22 -0000 (UTC) Organization: A noiseless patient Spider Lines: 55 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Injection-Date: Tue, 16 Jul 2024 15:24:22 +0200 (CEST) Injection-Info: dont-email.me; posting-host="022eb7690727c66a55916ff0843f867b"; logging-data="1361102"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18EmKLr7XvNxzcceYFVeICj" User-Agent: XanaNews/1.18.1.6 Cancel-Lock: sha1:j9sFwQ5ByoGiRIQjBc9g+GgSMH4= Bytes: 2285 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)))))