;; The first three lines of this file were inserted by DrScheme. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-beginner-reader.ss" "lang")((modname 22.7.1) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) ; Worked exercise 22.7.1 ; count-e : string -> number (check-expect (count-e "") 0) (check-expect (count-e "a") 0) (check-expect (count-e "e") 1) (check-expect (count-e "ab") 0) (check-expect (count-e "ae") 1) (check-expect (count-e "ea") 1) (check-expect (count-e "ee") 2) (check-expect (count-e "Tweedledum and Tweedledee were going to a fair") 10) (define (count-e str) (cond [(empty-string? str) 0 ] [(non-empty-string? str) ; str non-empty string ; (first-char str) length-1 string ; (chop-first-char str) string ; (count-e (chop-first-char str)) (cond [(string=? (first-char str) "e") (+ 1 (count-e (chop-first-char str)))] [else (count-e (chop-first-char str))])])) ; assuming you've written empty-string?, non-empty-string?, first-char, and chop-first-char ; Alternative approach: #| (define (count-e str) (count-matches #\e (string->list str))) |# ; assuming you've written count-matches already