• Re: [newbie] I need a function for read user input.

    From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp,comp.lang.scheme on Mon Jul 7 20:19:27 2025
    From Newsgroup: comp.lang.scheme

    B. Pym wrote:

    Another way:

    (use srfi-42) ; list-ec

    (list-ec (:port word input) word)

    Shorter:

    (define input (current-input-port))

    (list-ec (: word input) word)

    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp,comp.lang.scheme on Mon Jul 7 20:36:28 2025
    From Newsgroup: comp.lang.scheme

    B. Pym wrote:

    Gareth McCaughan wrote:

    (loop for word = (read input nil) while word collect word)

    which even the strictest LOOP-hater would have to agree
    is clearer.

    It's shorter in Gauche Scheme:

    (use gauche.generator)

    (generator->list (cut read input))


    Another way:

    (use srfi-42) ; list-ec

    (list-ec (:port word input) word)

    Using the old-fashioned and primitive "do":

    (do ((r () (cons x r))
    (x 0 (read)))
    ((eof-object? x) (cdr (reverse r))))
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp,comp.lang.scheme on Mon Jul 7 21:47:41 2025
    From Newsgroup: comp.lang.scheme

    B. Pym wrote:

    B. Pym wrote:

    Gareth McCaughan wrote:

    (loop for word = (read input nil) while word collect word)

    which even the strictest LOOP-hater would have to agree
    is clearer.

    It's shorter in Gauche Scheme:

    (use gauche.generator)

    (generator->list (cut read input))


    Another way:

    (use srfi-42) ; list-ec

    (list-ec (:port word input) word)

    Using the old-fashioned and primitive "do":

    (do ((r () (cons x r))
    (x 0 (read)))
    ((eof-object? x) (cdr (reverse r))))

    Using a slightly enhanced do:

    (call-with-input-file "data.bak"
    (lambda(input)
    (do. ((word (read input) <>)
    (r () (cons word r)))
    ((? word) @ r))))

    It's not too much longer than the LOOP:

    (loop for word = (read input nil) while word collect word)
    (do. ((word (read input) <>) (r () (cons word r))) ((? word) @ r))


    Explanation:

    (call-with-input-file "data.bak"
    (lambda(input)
    (do. ((word (read input) <>)

    The "<>" means repeat the preceding expression.

    (r () (cons word r)))
    ((? word) @ r))))

    The "?" means test word to see if it is an empty list
    or the eof-object.

    If the value being returned is preceded by "@", then
    it is reversed.


    Another example:

    (do. ((r () (cons (pop! xs) r))
    (xs '(a b c)))
    ((? xs) @ r))

    ===>
    (a b c)
    --- Synchronet 3.21a-Linux NewsLink 1.2