Re: Read-from-string

Liste des GroupesRevenir à cl lisp 
Sujet : Re: Read-from-string
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lisp
Date : 16. Jul 2024, 15:24:22
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v75sa5$19h6e$1@dont-email.me>
User-Agent : XanaNews/1.18.1.6
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)))))

Date Sujet#  Auteur
16 Jul 24 o Re: Read-from-string1B. Pym

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal