title: Scheme - Map function for applying a function to elements in a nested list
body: <p>I'm trying to write a mapping function in scheme that applies a function to each value in a nested list.</p>
  
  <p>For example, <code>(map number? '(3 (2 A) 2 Z)</code> should return <code>(#t (#t #f) #t #f)</code></p>
  
  <p>Here's what I have so far:</p>
  
  <pre><code>(define (map fun lst)
      (if (null? lst) '()
          (if (list? (car lst)) 
              (cons (map fun (car lst)) (map fun (cdr lst)))
                   (cons (fun (car lst)) (map fun (cdr lst))))))
  </code></pre>
  
  <p>It works if the nested list is at the front of the list. For example <code>(map number? '((3 A) 2 Z))</code> correctly returns <code>((#t #f) #t #f)</code></p>
  
  <p>The problem occurs when the nested list occurs after another element in the original list.
  For example <code>(map number? '(3 A (2 Z)))</code> incorrectly returns <code>(#t #f #f)</code> [The result should be <code>(#t #f (#t #f))</code>] </p>
  
  <p>How can I change my algorithm to correct this?</p>