10 POINTS |
Problem 3. Develop the program count, which counts how many times two strings occur in the given list. It consumes two strings and a list of strings; the result is a number.
;; List of string: A Los is one of: ;; -- empty ;; -- (cons String Los)
Solution:
;; count: String String Los -> Number [PT 1] ;; count how often left and right occur in los [PT 1] (define (count left right los) ; [PT 1] (cond ;; [PT 2: for two cond lines] [(empty? los) 0] ;; [PT 1, for base case] [else (cond ;; [PT 2, for correctness in this case] [(or (string=? (first los) left) (string=? (first los) right)) (+ 1 (count left right (rest los)))] [else (count left right (rest los))])])) ;; Examples/tests: ;; [PT 2; for a non-trivial example or two examples] (equal? (count "le" "ri" (cons "he" (cons "le" (cons "ri" (cons "le" (cons "ri" empty)))))) 4)