title: Recursive Atom Counter in Scheme
body: <p>I need to come up with a scheme function that counts the occurence of an atom within a list. I successfully implemented a simple version but it only works with non-nested lists:</p>
  
  <pre><code>(define (foo x atom)
      (cond ((null? x) 0)
            ((eqv? (car x) atom) (+ 1 (count (cdr x) atom)))
            (else (count (cdr x) atom))))
  </code></pre>
  
  <p>I then attempted a nested-list version of it. However, I recieve the error: </p>
  
  <pre><code>argument of wrong type [car]
  
  (car *whatever variable in list*)
  </code></pre>
  
  <p>The code is as follows:</p>
  
  <pre><code>(define (foo x atom)
    (cond
     ((null? x) 0)
     ((not (list? (car x)))
      (cond
       ((eqv? (car x) atom) (+ 1 (foo (cdr x) atom)))
       (else (foo (cdr x) atom))))
     (else (+ (foo (car x) atom) (foo (cdr x) atom)))))
  </code></pre>
  
  <p>It fails when I pass a a list such as:</p>
  
  <pre><code>(foo '(1 2 3 1 1 4 1) 1)
  </code></pre>