title: Length function in " The Seasoned Schemer"
body: <p>I have been reading The Seasoned Schemer and i came across this definition of the <em>length</em> function</p>
  
  <pre><code>(define length
    (let ((h (lambda (l) 0)))
      (set! h (L (lambda (arg) (h arg))))
      h))
  </code></pre>
  
  <p>Later they say : </p>
  
  <blockquote>
    <p>What is the value of (L (lambda (arg) (h arg)))? It is the function</p>
  </blockquote>
  
  <pre><code>(lambda (l)
    (cond ((null? l) 0)
       (else (add1 ((lambda (arg) (h arg)) (cdr l))))))
  </code></pre>
  
  <p>I don't think I comprehend this fully. I guess we are supposed to define <strong><em>L</em></strong> ourselves as an excercise. I wrote a definition of <strong>L</strong> within the definition of <em>length</em> using letrec. Here is what I wrote:</p>
  
  <pre><code>(define length
    (let ((h (lambda (l) 0)))
      (letrec ((L
                (lambda (f)
                  (letrec ((LR
                            (lambda (l)
                              (cond ((null? l) 0)
                                    (else
                                     (+ 1 (LR (cdr l))))))))
                    LR))))                  
      (set! h (L (lambda (arg) (h arg))))
      h)))
  </code></pre>
  
  <p>So, <strong>L</strong> takes a function as its argument and returns as value another function that takes a list as its argument and performs a recursion on a list. Am i correct or hopelessly wrong in my interpretation? Anyway the definition works</p>
  
  <pre><code> (length (list 1 2 3 4))  =&gt; 4
  </code></pre>