title: Binary Trees in Scheme
body: <p>Consider the following BNF defining trees of numbers. 
  Notice that a tree can either be a leaf, a node-1 with one subtrees, or a node-2
  with two subtrees.</p>
  
  <pre><code>tree ::= (’leaf number)
  | (’node-1 tree)
  | (’node-2 tree tree)
  </code></pre>
  
  <p>a. Write a template for recursive procedures on these trees.</p>
  
  <p>b. Define the procedure (leaf-count t) that returns the
  number of leaves in t</p>
  
  <pre><code>&gt; (leaf-count ’(leaf 5))
  
  1
  
  &gt; (leaf-count ’(node-2 (leaf 25) (leaf 17)))
  
  2
  
  &gt; (leaf-count ’(node-1
  (node-2 (leaf 4)
  (node-2 (leaf 2) (leaf 3)))))
  
  3
  </code></pre>
  
  <p>Here's what I have so far:</p>
  
  <pre><code>;define what a leaf, node-1, and node-2 is
  (define leaf list)
  (define node-1 list)
  (define node-2 list)
  
  ;procedure to decide if a list is a leaf or a node
  (define (leaf? tree) (number? (car tree)))
  (define (node? tree) (pair? (car tree)))
  
  (define (leaf-count tree)
   (cond ((null? tree) 0)
          ((number? tree) 0)
          ((leaf? tree) 1)
          (else (+ (leaf-count (car tree))
                   (leaf-count (cdr tree))))))
  </code></pre>
  
  <p>It looks like it should run just fine, but when I try to run it using a simple test case like</p>
  
  <pre><code>(leaf-count '(leaf 5))
  </code></pre>
  
  <p>I get the following error message:</p>
  
  <blockquote>
    <p>car: expects argument of type pair; given leaf</p>
  </blockquote>
  
  <p>What does this error message mean? I am defining a leaf as a list. But for some reason, it's not seeing that and gives me that error message.</p>