;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; U. C. Berkeley ;; ;; EECS Computer Science Division ;; ;; CS3 Lecture 11 Answers ;; ;; (Advanced Recursion Questions) ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Write a procedure grow-mountains, ;; that, given n, returns a ;; sentence of length n, where ;; each word is a "mountain" ;; counted up from 1 to n and ;; down to 1 again. ;; ;; Examples: (grow-mountains 0) ;; ==> () (grow-mountains 4) ;; ==> (1 121 12321 1234321) ;; grow-mountains, embedded (define (grow-mountains n) (if (= n 0) '() (se (grow-mountains (- n 1)) (mountain n)))) ;; grow-mountains, iterative (tail recursive) (define (grow-mountains n) (grow-mountains-helper n '())) (define (grow-mountains-helper n ans) (if (= n 0) ans (grow-mountains-helper (- n 1) (se (mountain n) ans)))) ;; mountain, embedded (define (mountain n) (mountain-helper 1 n)) (define (mountain-helper i n) (if (= n i) n (word i (mountain-helper (+ i 1) n) i))) ;; mountain, iterative (tail-recursive) (define (mountain n) (mountain-helper (- n 1) n)) (define (mountain-helper i ans) (if (= i 0) ans (mountain-helper (- i 1) (word i ans i)))) ;; Testing (mountain 4) ;; ==> 1234321 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Write a procedure binary to generate ;; the possible binary numbers ;; given n (n > 0) bits. Each "bit" is ;; a 0 or 1. ;; ;; Examples (binary 1) ;; ==> (0 1) (binary 2) ;; ==> (00 01 10 11) (binary 3) ;; ==> (000 001 010 011 100 101 110 111) (define (binary n) (if (= n 1) '(0 1) (se (every-prefix 0 (binary (- n 1))) (every-prefix 1 (binary (- n 1)))))) ;; every-prefix, hof version (define (every-prefix pre s) (every (lambda (w) (word pre w)) s)) ;; every-prefix, embedded version (define (every-prefix pre s) (if (empty? s) '() (se (word pre (first s)) (every-prefix pre (bf s))))) ;; Testing (every-prefix 'dr- '(spock ruth dre j)) ;; ==> (dr-spock dr-ruth dr-dre dr-j) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Write strip-most-popular-letter, ;; which removes the most common letter ;; from every word in the given sentence ;; ;; Examples: (strip-most-popular-letter '(cs3 is the best class)) ;; ==> (c3 i th bet cla) ;; Solution: ;; ;; I believe the hardest part of this question ;; is NOT the actual recursion problems but ;; breaking it up into the component pieces. ;; ;; I've shown the solution based on using ;; every, keep and accumulate, which could ;; be substituted with appropriate recursive ;; functions which use these patterns. ;; First, find the most popular letter, ;; then remove it from every word. (define (strip-most-popular-letter s) (let ((mpl (most-popular-letter-s s))) (every (lambda (w) (remove-letter mpl w)) s))) ;; Remove the letter mpl from word w (define (remove-letter mpl w) (keep (lambda (l) (not (equal? l mpl))) w)) ;; Testing (remove-letter 'a 'abracadabra) ;; ==> brcdbr ;; Find the most popular letter in the sentence ;; by smushing the entire sentence into a word ;; and then finding the most popular letter in ;; a word (easier) (define (most-popular-letter-s s) (most-popular-letter-w (accumulate word s))) (most-popular-letter-s '(a bra cad a bra)) ;; ==> a ;; Find the most popular letter in the word by ;; first creating a sentence of the number of ;; times that letter appears in the whole ;; sentence, like (3 1 3 3 1) for "poppy", ;; then finding the max of that (here, 3) ;; then finding the index of that max (here, 1) ;; then finding the index-th letter in that word ;; (here, p). ;; ;; This is by far the heart of the program. (define (most-popular-letter-w w) (let ((how-many-s (every (lambda (l) (appearances l w)) w))) (item (find-index (accumulate max how-many-s) how-many-s) w))) ;; Testing (most-popular-letter-w 'abracadabra) ;; ==> a ;; Find-index searches for the numeric index ;; where a word appears in a sentence. (or, ;; in this case, a letter in a word) (define (find-index w s) (find-index-helper w s 1)) (define (find-index-helper w s i) (if (equal? w (first s)) i (find-index-helper w (bf s) (+ i 1)))) ;; Testing (find-index 'p 'poppy) ;; ==> 1 (find-index 'class '(cs3 is the best class)) ;; ==> 5