;;; answer.scm ; Write an answer procedure. ; Write a procedure named answer that, given a sentence that represents ; a question, returns a simple answer to that question. (A question's ; last word ends with a question mark.) If the argument sentence is not ; a question, answer should merely return the argument unchanged. ; ; Here's how the answer procedure should process different kinds ; of questions. ; * Given ( am i ...? ), answer should return ( you are ...). ; * Given ( are you ...? ), answer should return ( i am ...). ; * Given ( some-other-word i ... ? ), answer should return ; ( you some-other-word ...). ; * Given ( some-other-word you ... ? ), answer should return ; ( i some-other-word ...). ; * Given any other question, answer should return the result ; of replacing the question mark by a period. ; ; IMPORTANT: Scheme has some problems with periods. (More correctly, ; a period means something special in Scheme, and different than ; punctuation that comes at the end of a english sentence.) You ; need to put a period in double quotes ("."). For example, if you ; want to put a period at the end of the word "weasel," you would ; use this code: (word 'weasel ".") ;;;;;;;;; ; as above ; lots of helpers, whew! (define (answer sent) (cond ((not (question? sent)) ; not a question, just return the sent sent) ((first-two-equal? 'am 'i sent) ; (am i ...) (se 'you 'are (middle-part sent) (turn-?-to-. (last sent)))) ((first-two-equal? 'are 'you sent) ; (are you ...) (se 'i 'am (middle-part sent) (turn-?-to-. (last sent)))) ((equal? 'i (second sent)) ; ( i ...) (se 'you (first sent) (middle-part sent) (turn-?-to-. (last sent)))) ((equal? 'you (second sent)) ; ( you ...) (se 'i (first sent) (middle-part sent) (turn-?-to-. (last sent)))) (else ; some other question (se (bl sent) (turn-?-to-. (last sent)))) ) ) #| ;; tests for answer (answer '(this isnt a question!)) ; (this isnt a question!) (answer '(this is a question?)) ; (this is a question.) (answer '(am i the dream program?)) ; (you are the dream program.) (answer '(am i the dream program.)) ; (am i the dream program.) (answer '(am eye the dream program?)) ; (am eye the dream program.) (answer '(are you commented?)) ; (i am commented.) (answer '(do i like rock cod?)) ; (you do like rock cod.) (answer '(do you like mecury poisoning?)) ; (i do like mercury poisoning.) |# ;;; returns true if the last word in the sentence ends in a question mark (define (question? sent) (and (sentence? sent) (not (empty? sent)) ; if we don't test for empty, the next ; line might fail (equal? (last (last sent)) "?") )) #| ;; tests for question? (question? '()) ; #f (question? '(I am named fred)) ; #f (question? '(I am named?)) ;#t |# ;;; returns #t w1 and w2 are the first two words inside sent (define (first-two-equal? w1 w2 sent) (and (not (empty? sent)) (not (empty? (bf sent))) ; ensures that there are at least two words ; so that the following doesn't crash (equal? w1 (first sent)) (equal? w2 (first (bf sent))))) #| tests for first-two-equal? (first-two-equal? 'are 'you '(are you fred)) ; #t (first-two-equal? 'are 'you '(you are fred)) ; #f (first-two-equal? 'are 'you '()) ; #f |# ;; returns the sentence without the first two words and the last word (define (middle-part sent) (if (>= (count sent) 3) (bf (bf (bl sent))) '() ; hmmm, not clear what should be returned here... )) #| (middle-part '(four score and seven years ago?)) ; (and sever years) (middle-part '(in the eeeeeee-ven-innnngg)) ; () (middle-part '(joe)) ; () |# ;; returns a word with the question mark (at the end) turned into a period (define (turn-?-to-. wd) (if (equal? (last wd) "?") (word (bl wd) ".") wd)) #| ; tests for turn-?-to-. (turn-?-to-. 'joe) ; "joe", although this shouldn't matter (turn-?-to-. 'joe?) ; "joe." (turn-?-to-. "?") ; "." |# ;; returns the second word in the sentence (define (second sent) (first (bf sent)))