问题
I'll be upfront: this is Homework. The following code defines a function countup
, called as:
(countup "file1")
It appears that the loop is running indefinitely. Why, and how can I fix this?
(define stats
(lambda (srcf)
(begin
(define in (open-input-file srcf))
(let loop (
(l 0)
(w 0)
(c 0)
(char (read-char in)))
(case char
((#\newline)
(loop (+ l 1) w(+ c 1) (read-char in)))
((#\space #\tab)
(loop l (+ w 1) (+ c 1) (read-char in)))
(else (loop l w (+ c 1) (read-char in))))
)
(close-input-port in)
(display l)
(display " ")
(display w)
(display " ")
(display c)
(newline)
'()))
)
;; srcf = source text file
(define countup
(lambda (srcf lstf)
(stats srcf)
)
)
回答1:
A program that reads characters from a file must have this condition somewhere:
(cond ((eof-object? the-char)
'finished)
...)
Take a look at this answer to see a procedure similiar to the one you're writing, it might be helpful.
来源:https://stackoverflow.com/questions/16063788/file-stats-in-scheme