File Stats in Scheme

岁酱吖の 提交于 2019-12-23 18:13:24

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!