Problem 11.2 from the book.
Note that I have changed some of the code that is given in the book so that cond is used instead of if and (count-ums 1) calls (count-ums 0).
(#%require racket/trace) (define (count-ums0 sent) 0) (define (count-ums1 sent) (cond ((equal? 'um (first sent)) (+ 1 (count-ums0 (bf sent)))) (else (count-ums0 (bf sent))))) (define (count-ums2 sent) (cond ((equal? 'um (first sent)) (+ 1 (count-ums1 (bf sent)))) (else (count-ums1 (bf sent))))) (define (count-ums3 sent) (cond ((equal? 'um (first sent)) (+ 1 (count-ums2 (bf sent)))) (else (count-ums2 (bf sent))))) ; count-ums takes a sentence as its input and returns the number of times ; the word 'um appears in the sentence ; > (count-ums '(cow um i forgot um what i was um going to say)) ; 3 ; ; Recursive procedure is a procedure that calls itself ; It has two properties: ; * At least one recursive call (i.e., where it calls itself) ; * At least one base case (i.e., where does the function stop) (define (count-ums sent) (cond ((empty? sent) 0) ; if the first word is 'um... ((equal? (first sent) 'um) (+ 1 (count-ums (bf sent)))) ; if it isn't... (else (count-ums (bf sent))))) (trace count-ums)