Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

Q: How can you tell when a Burroughs salesman is lying? A: When his lips move.


comp / comp.lang.lisp / Re: Read-from-string

SubjectAuthor
o Re: Read-from-stringB. Pym

1
Subject: Re: Read-from-string
From: B. Pym
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Tue, 16 Jul 2024 13:24 UTC
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Nobody447095@here-nor-there.org (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: <v75sa5$19h6e$1@dont-email.me>
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=
View all headers

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)))))

1

rocksolid light 0.9.8
clearnet tor