さかもとのブログ

つらつらと

call-with-input-file,call-with-output-file

(define (count-chars filename)
  (call-with-input-file filename
    (lambda (in)
      (let loop ((c (read-char in))
                 (count 0))
        (cond [(eof-object? c) count]
              [else (write c)
                    (loop (read-char in) (+ count 1))])))))
(count-chars "read-char.scm")

(define (write-number filename)
  (call-with-output-file filename
    (lambda (out)
      (let loop ((count 10))
        (cond [(< count 0) count]
              [else (format out "number = ~a\n" count)
                    (loop (- count 1))])))))
(write-number "number.scm")