Lara
Lara

Reputation: 3164

How to read a line of input in Chez-Scheme?

I can't find out how to do this. In previous implementations read-line was available but for some reason it isn't in Chez.

How do I just read a line of input?

Upvotes: 2

Views: 1504

Answers (2)

dfours
dfours

Reputation: 141

Chez Scheme is the R6RS implementation. Use the R6RS get-line instead of the R7RS read-line.

Upvotes: 3

user448810
user448810

Reputation: 17876

I have a read-line in my Standard Prelude; it handles end-of-line as carriage-return, line-feed, or both in either order:

(define (read-line . port)
  (define (eat p c)
    (if (and (not (eof-object? (peek-char p)))
             (char=? (peek-char p) c))
        (read-char p)))
  (let ((p (if (null? port) (current-input-port) (car port))))
    (let loop ((c (read-char p)) (line '()))
      (cond ((eof-object? c) (if (null? line) c (list->string (reverse line))))
            ((char=? #\newline c) (eat p #\return) (list->string (reverse line)))
            ((char=? #\return c) (eat p #\newline) (list->string (reverse line)))
            (else (loop (read-char p) (cons c line)))))))

Upvotes: 3

Related Questions