t3x.org / nss / read-line.html

(Nils' Scheme Snippets)

 
Paren matching: ON  |  Category: io  |  Overview  |  Scheme Books  |  License
 

(read-line . port) => string

 
Purpose
Read a line from an input port.
 
Arguments
port port to read, default = current input port
 
Example
(read-line) hello, world => " hello, world"
(define (read-line . port)
  (letrec
    ((collect-chars
       (lambda (c s)
         (cond
           ((eof-object? c)
             (if (null? s) c (list->string (reverse s))))
           ((char=? c #\newline)
             (list->string (reverse s)))
           (else (collect-chars (apply read-char port)
                                (cons c s)))))))
    (collect-chars (apply read-char port) '())))

Copyright (C) 2007 Nils M Holm <nmh @ t3x . org>