t3x.org / sketchy / prog / read-line.html
SketchyLISP Stuff Copyright (C) 2006 Nils M Holm

read-line

Conformance: R5RS

Purpose: Read a newline-terminated sequence of characters. Return a string containing the characters read.

Implementation:

(define (read-line)
  (letrec
    ((collect-chars (lambda (c s)
      (cond ((or (eof-object? c) (char=? c #\newline))
          (apply string (reverse s)))
        (#t (collect-chars (read-char) (cons c s))))))
    (first-char (read-char)))
    (cond ((eof-object? first-char) first-char)
      (#t (collect-chars first-char '())))))

Example:

(read-line) hello world 
=> " hello world "