2009-11-19 64 views
2

我移植从LISP一些代码,但我被困在这部分(显然这是麻省理工学院的方案)Lisp代码的解释

(define (end-of-sentence? word) 
    (and (or (char-exist-last? word '#\.) 
      (char-exist-last? word '#\!) 
      (char-exist-last? word '#\?)) 
     (not (initial? word)) 
     (or (eq? (peek-char) '#\Space) ;;peek- so test for linefeed isn't affected 
      (eq? (peek-char) '#\n) ;;note- test for space isn't neccessary 
      (eq? (read-char) '#\t)))) 


;;counts the number of sentences in a given file and 
;;stops at the given number 
;;returns true once at that position, returns error if eof 
(define (goto-sentence? file stop) 
    (define (c-g-iter num) 
    (cond ((= num stop) 
      #t) 
      ((end-of-sentence?) 
      (c-g-iter (+ num 1))) 
      ((not (char-ready?)) 
      (error "EOF reached, number to large in goto-sentence?: " stop)) 
      (else 
      (c-g-iter num)))) 
    (begin 
    (open-path file) 
    (c-g-iter 1))) 

当然我可以只跳过和实施意见说什么它确实,但只是想确保背景中没有魔术发生。所以...这个功能如何工作 - 它实际上在哪里阅读角色?它是否和我想象的一样丑陋,是否会在end-of-sentence?的最后一次检查中消耗字符作为副作用?还是char-ready?实际上读了什么?

但是再次 - (end-of-sentence?) (c-g-iter (+ num 1))是什么意思,因为我不期望c-g-iter返回一个单词。

回答

2

我没有计划程序员,但现在看来,字符被消耗在read-charsource

end-of-sentence?,另一方面似乎越来越没有所谓的参数,即使它被声明为一个。我认为它依次调用函数容忍任何系统提供未指定的参数

两人(end-of-sentence?) (c-g-iter (+ num 1))是一个参数cond,你能想到像一个开关或简洁的if/else(nil?);第一部分是测试(end-of-sentence?),第二部分是要执行的内容如果为真(c-g-iter (+ num 1))

1

只需将我的声音添加到合唱中;也许我可以提供一些见解。 (1)所以我不能确定它们是做什么的。所以我不确定它们是做什么的。

这就是说,我认为end-of-sentence?需要一个字符串(word,所以它应该是一个单词),并返回true,如果它的最后一个字符是'!','?或'。',并且该单词之后的下一个字符是空格,换行符或制表符。另外,看着intial,它可能不是句子中的第一个单词(例如,'A',例如,不应该返回true,而应该是'A dog'。)

read-char确实'消耗字符' - “返回从输入端口可用的下一个字符,更新输入端口以指向以下字符。” (Google搜索“读取字符方案”得到MIT-Scheme input procedures。)

每个相同的源代码char-ready?的工作方式如下:“返回#t,如果输入端口上的字符已准备就绪,否则返回#f。

希望这是至少有人启迪!

(1)MIT-Scheme Reference