fork(1) download
  1. ; count all matches
  2.  
  3. (define (count-all-matches needle haystack)
  4. (let loop ((needle (string->list needle))
  5. (haystack (string->list haystack)))
  6. (cond ((null? needle) 1)
  7. ((null? haystack) 0)
  8. ((char=? (car needle) (car haystack))
  9. (+ (loop (cdr needle) (cdr haystack))
  10. (loop needle (cdr haystack))))
  11. (else (loop needle (cdr haystack))))))
  12.  
  13. (display (count-all-matches "cat" "catapult")) (newline)
  14. (display (count-all-matches "cap" "catapult")) (newline)
  15. (display (count-all-matches "cut" "catapult")) (newline)
  16. (display (count-all-matches "cup" "catapult")) (newline)
Success #stdin #stdout 0s 7272KB
stdin
Standard input is empty
stdout
3
2
1
0