Workshop on Scheme

Answers to exercises: Friday, July 21

(define (character-count filename)
  (call-with-input-file
   filename
   (lambda (source)
     (let loop ((count 0)
                (ch (read-char source)))
       (if (eof-object? ch)
           count
           (loop (+ count 1) (read-char source)))))))


(define (read-line source)
  (let loop ((chars '())
             (ch (read-char source)))
    (if (char=? ch #\newline)
        (apply string (reverse chars))
        (loop (cons ch chars) (read-char source)))))


This document is available on the World Wide Web as

http://www.math.grin.edu/~stone/events/scheme-workshop/Friday-answers.html


created July 21, 1995
last revised July 21, 1995

John David Stone (stone@math.grin.edu)