Problem 3.   An old Countess from Europe wishes to buy a program from your company that counts how often a name starts with "de" in her (huge) list of friends. Your manager wants to make a quick buck and therefore asks you to design the function count, which consumes a list of strings and counts how many of them start with "de". Since he was in a good mood, he also supplied the essential data definition:

  ;; List of string: 
  ;;
  ;; A LOS is one of:
  ;; -- empty 
  ;; -- (cons String LOS)

and then walked off mumbling substring, 0, 2 and things like that.



Solution:

  ;; Grader: Ryan

  ;; You may assume that strings are 2 chars or longer. 

  ;; count: LOS -> Number [PT 1]
  ;; count how often a string starting with 
  ;; "de" occurs in los [PT 1]
  (define (count los) 
    (cond
      ;; [PT 2: for two cond lines]
      [(empty? los) 0] ;; [PT 1, for base case]
      [else (cond ;; [PT 3: 1 for natural recursions,
                  ;;        1 for nested conditional, 
                  ;;        1 for correctness]
              [(string=? (substring (first los) 0 2) "de")
               (+ 1 (count (rest los)))]
              [else 
               (count (rest los))])]))

  ;; Examples/tests:
  ;; [PT 2; for a non-trivial example or two examples]
  (equal? (count
            (cons "de he"
              (cons "le"
                (cons "de ri"
                  (cons "le"
                    (cons "ri" empty))))))
            2)