chickadee » foof-loop » IN-PORT

(IN-PORT <input-port> [<reader> [<eof?>]])syntax

Usage:

 (FOR <datum> (IN-PORT <input-port> [<reader> [<eof?>]]))

Iterates for successive data read from <input-port>, binding the variable <datum> to the datum read by applying the value of <reader>. Iteration terminates when the datum read satisfies the predicate <eof?>.

<Input-port> is usually an input port. <Reader> and <eof?>, if supplied, must be unary procedures. The default value of <reader> is READ-CHAR, and the default value of <eof?> is EOF-OBJECT?. <Input-port>, <reader>, and <eof?> are all evaluated once before the loop begins.

<Datum> is an entry variable.

   (define (read-line input-port)
     (let ((initial (peek-char input-port)))
       (if (eof-object? initial)
           initial
           (loop ((for char (in-port input-port))
                  (until (char=? char #\newline))
                  (with chars '() (cons char chars)))
             => (list->string (reverse chars))))))
   
   (define (read-all input-port)
     (loop ((for datum (in-port input-port read))
            (with data '() (cons datum data)))
       => (reverse data)))