views:

152

answers:

2

I'm porting some code from lisp, but I got stuck at this part (apparently that's for mit-scheme)

(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)))

Of course I could just skip that and implement what the comments say it does, but just wanted to be sure there's no magic happening in the background. So... how does this function even work -- where is it actually reading the characters? Is it as ugly as I think and does it consume the characters as a side-effect in the last check in end-of-sentence?? Or does the char-ready? actually read something?

But then again - what does (end-of-sentence?) (c-g-iter (+ num 1)) mean, as I don't expect c-g-iter to return a word.

+1  A: 

I'm no scheme programmer, but it appears that characters are being consumed in read-char source

end-of-sentence? on the other hand appears to be getting called without a parameter, even though it is declared to take one. I assume that the functions it calls in turn are tolerant to whatever the system provides for unspecified parameters (nil?)

The pair (end-of-sentence?) (c-g-iter (+ num 1)) is a parameter to cond, which you can think like a switch or concise if/else; the first part is a test (end-of-sentence?), and the second is what to execute if true (c-g-iter (+ num 1))

Justin Love
A: 

Just adding my voice to the chorus; maybe I can provide some insight.

Some functions that are in these functions are not standard mit-sheme, such as char-exist-last? and initial?.(1) So I can't be sure what they do.

That being said, I think that end-of-sentence? takes in a string (word, so it should be a word) and returns true if its last character is a '!', '? or '.', and the next character after the word is a space, newline or tab character. Also, looking at intial, it probably can't be the first word in the sentence ('A.', for example, shouldn't return true, but 'A dog.' should.)

read-char does indeed 'consume characters' - "Returns the next character available from input-port, updating input-port to point to the following character." (Googled 'read-char mit scheme' to get MIT-Scheme input procedures.)

Per the same source char-ready? works like so: "Returns #t if a character is ready on input-port and returns #f otherwise."

Hope this is at least someone enlightening!

(1) MIT-Scheme Reference

Isaac Hodes