August Feng

Read lines from a port into a list

Just some elegant code (that took me way too long to learn and write) for reading text into a list.

  (use-modules (ice-9 popen)
               (ice-9 rdelim))

  (define (read-lines port)
    (let read-line-into-list ((lines '()))
      (cond ((read-line port) string? => (lambda (line) (read-line-into-list (cons line lines))))
            (else (reverse lines)))))

  (let ((port (open-input-pipe "echo foo; echo bar")))
    (read-lines port))