; count all matches

(define (count-all-matches needle haystack)
  (let loop ((needle (string->list needle))
             (haystack (string->list haystack)))
    (cond ((null? needle) 1)
          ((null? haystack) 0)
          ((char=? (car needle) (car haystack))
            (+ (loop (cdr needle) (cdr haystack))
               (loop needle (cdr haystack))))
          (else (loop needle (cdr haystack))))))

(display (count-all-matches "cat" "catapult")) (newline)
(display (count-all-matches "cap" "catapult")) (newline)
(display (count-all-matches "cut" "catapult")) (newline)
(display (count-all-matches "cup" "catapult")) (newline)