HW5 Solutions Answer to exercise 8.7: (define (letter-count sent) (accumulate + (every count sent))) Answer to exercise 8.8: (define (exaggerate sent) (every exaggerate-word sent)) (define (exaggerate-word wd) (cond ((equal? wd 'good) 'great) ((equal? wd 'bad) 'terrible) ((equal? wd 'great) 'awesome) ((equal? wd 'terrible) 'grotty) ((number? wd) (* 2 wd)) (else wd))) Answer to exercise 8.9: If you use WORD or SENTENCE as the first argument to EVERY, and the second argument is a sentence, you'll always get back that sentence. If it's a sentence of numbers, +, *, MAX, and MIN would also work. Also, you could define your own procedure, as in (define (identity x) x) > (every identity '(the identity function works too)) (THE IDENTITY FUNCTION WORKS TOO) The only "fair" primitive that will work for KEEP is WORD?, since by definition every word of a sentence is a word. You could also use a made-up procedure: (define (true x) #t) > (keep true '(all the words pass this test)) (ALL THE WORDS PASS THIS TEST) However, since any non-false value counts as true to Scheme, many one-argument functions would work, such as WORD or COUNT. But that's a bizarre way to use KEEP. With ACCUMULATE, there's only one possible function argument: SENTENCE. Answer to exercise 8.14: (define (subword wd start end) ((repeated bf (- start 1)) ((repeated bl (- (count wd) end)) wd))) Suppose our word is POLYTHENE, and we want letters 5 through 8. Since the word is nine letters long, and the last letter we want is letter number 8, we have to BUTLAST the word once. (Nine minus eight is one.) In general, we do ((repeated bl (- (count wd) end)) wd) That gives us a word that ends in the right place, but might have some extra letters at the front. In the particular case, since we want to start with letter number 5, we must take the BUTFIRST 4 times. (If you wanted to start with letter number 1 you'd BUTFIRST zero times.) Answer to exercise 9.2: (define second (lambda (stuff) (first (bf stuff)))) (define make-adder (lambda (num) (lambda (x) (+ num x)))) Answer to exercise 9.6: (define (sentence-version fn) (lambda (sent) (every fn sent))) Answer to exercise 9.7: (define (letterwords letter sent) (keep (lambda (wd) (member? letter wd)) sent)) Answer to exercise 9.13: (define (compose f g) (lambda (x) (f (g x))))