• Re: read - comma

    From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp on Sun Jul 13 07:10:08 2025
    From Newsgroup: comp.lang.lisp

    Frank Buss wrote:

    How to read a comma separated list from a file.
    For example, " 2, 3, 5, 6,".

    I assume you want to read only one line, every item is an integer and
    empty entries should be ignored (after your last comma). Here is a
    recursive solution:

    (defun split (string delimiter)
    (when (= 0 (length delimiter)) (error "delimiter length = 0"))
    (let ((start (search delimiter string)))
    (if (not start)
    (if (= 0 (length string))
    nil
    (list string))
    (let ((value (string-trim '(#\Space) (subseq string 0 start)))
    (rest (subseq string (+ start (length delimiter)))))
    (if (= 0 (length value))
    (split rest delimiter)
    (cons value (split rest delimiter)))))))

    Gauche Scheme

    After converting all of the commas to spaces, repeatedly
    read from the string and put the numbers into a list
    (using the seldom seen "unfold").

    (use srfi-1) ;; unfold

    (with-input-from-string
    (string-map (lambda(c) (if (eq? c #\,) #\space c)) " 2, 3, 5, 6,")
    (lambda()
    (unfold eof-object? identity (lambda _ (read)) (read))))

    ===>
    (2 3 5 6)
    --- Synchronet 3.21a-Linux NewsLink 1.2