I seem to be having some trouble "getting" how to do certain tasks in lisp. I've been using clojure for a bit and am just picking up scheme (racket), and often times, when given a particular function to write, I find it difficult to write the code all as one function, and often find that the problem is simplified a lot in my mind if I break it up into several functions. Is this a good way of lisping, or am I going about things the wrong way? An example: I needed to write a function that would search for a given pattern inside of a larger list and return the number of occurrences (allowing overlapping). I came up with this:
(define (count-pattern pattern lst)
(if (null? lst) 0
(+ (if (check-pattern pattern lst) 1 0) (count-pattern pattern (cdr lst)))))
(define (check-pattern pattern lst)
(cond
((null? pattern) #t)
((null? lst) #f)
(else (and (equal? (car pattern) (car lst)) (check-pattern (cdr pattern) (cdr lst))))))
Is there a more elegant way to write this as one function that I'm not seeing, or is what I did acceptable in this case?