tweaked week3
[lambda.git] / week3.mdwn
index 990ef4b..f5f075a 100644 (file)
-[Give recursive definition for computing the length of a list.]
+##Computing the length of a list##
 
-Our fold-based implementation of lists, and Church's implementations of numbers, have a internal structure that mirrors common recursive operations we'd use lists and numbers for.
+How could we compute the length of a list? Without worrying yet about what lambda-calculus implementation we're using for the list, the basic idea would be to define this recursively:
 
-As we said, it takes some ingenuity to define functions like `extract-tail` or `predecessor` for these implementations, however it can be done. (And it's not *that* difficult.) Given those functions, we can go on to define other functions like numeric equality, subtraction, and so on, just by exploiting the structure already present in our implementation of lists and numbers.
+>      the empty list has length 0
+
+>      any non-empty list has length 1 + (the length of its tail)
+
+In OCaml, you'd define that like this:
+
+       let rec get_length = fun lst ->
+               if lst == [] then 0 else 1 + get_length (tail lst)
+       in ... (* here you go on to use the function "get_length" *)
+
+In Scheme you'd define it like this:
+
+       (letrec [(get_length (lambda (lst) (if (null? lst) 0 (+ 1 (get_length (cdr lst))))))]
+               ... ; here you go on to use the function "get_length"
+       )
+
+Some comments on this:
+
+1. `null?` is Scheme's way of saying `isempty`. That is, `(null? lst)` returns true (which Scheme writes as `#t`) iff `lst` is the empty list (which Scheme writes as `'()` or `(list)`).
+
+2. `cdr` is function that gets the tail of a Scheme list. (By definition, it's the function for getting the second member of an ordered pair. It just turns out to return the tail of a list because of the particular way Scheme implements lists.)
+
+What is the `let rec` in the OCaml code and the `letrec` in the Scheme code? These work like the `let` expressions we've already seen, except that they let you use the variable `get_length` *inside* the body of the function being bound to it---with the understanding that it will there refer to the same function that you're then in the process of binding to `get_length`. In OCaml:
+
+       let rec get_length = fun lst ->
+               if lst == [] then 0 else 1 + get_length (tail lst)
+       in get_length [20; 30]
+       (* this evaluates to 2 *)
+
+In Scheme:
+
+       > (letrec [(get_length 
+                       (lambda (lst) (if (null? lst) 0 (+ 1 (get_length (cdr lst))) ) )i
+               )] (get_length (list 20 30)))
+       ; this evaluates to 2
+       
+If you instead use an ordinary `let` (or `let*`), here's what would happen, in OCaml:
+
+       let get_length = fun lst ->
+               if lst == [] then 0 else 1 + get_length (tail lst)
+       in get_length [20; 30]
+       (* fails with error "Unbound value length" *)
+
+Here's Scheme:
+
+       > (let* [(get_length 
+                       (lambda (lst) (if (null? lst) 0 (+ 1 (get_length (cdr lst))) ) )
+               )] (get_length (list 20 30)))
+       ; fails with error "reference to undefined identifier: get_length"
+
+Why? Because we said that constructions of this form:
+
+       let get_length = A
+               in B
+
+really were just another way of saying:
+
+       (\get_length. B) A
+
+and so the occurrences of `get_length` in A *aren't bound by the `\get_length` that wraps B*. Those occurrences are free.
+
+We can verify this by wrapping the whole expression in a more outer binding of `get_length` to some other function, say the constant function from any list to the integer 99:
+
+       let get_length = fun lst -> 99
+       in let get_length = fun lst ->
+                       if lst == [] then 0 else 1 + get_length (tail lst)
+       in get_length [20; 30]
+       (* evaluates to 1 + 99 *)
+
+Here the use of `get_length` in `1 + get_length (tail lst)` can clearly be seen to be bound by the outermost `let`.
+
+And indeed, if you tried to define `get_length` in the lambda calculus, how would you do it?
+
+       \lst. (isempty lst) zero (add one (get-length (extract-tail lst)))
+
+we've defined all of `isempty`, `zero`, `add`, `one`, and `extract-tail` in earlier discussion. But what about `get-length`? That's not yet defined! In fact, that's the very formula we're trying here to specify.
+
+What we really want to do is something like this:
+
+       \lst. (isempty lst) zero (add one (... (extract-tail lst)))
+
+where this very same formula occupies the `...` position:
+
+       \lst. (isempty lst) zero (add one (
+               \lst. (isempty lst) zero (add one (... (extract-tail lst)))
+                       (extract-tail lst)))
+
+but as you can see, we'd still have to plug the formula back into itself again, and again, and again... No dice.
+
+So how could we do it? And how do OCaml and Scheme manage to do it, with their `let rec` and `letrec`?
+
+1.     OCaml and Scheme do it using a trick. Well, not a trick. Actually an impressive, conceptually deep technique, which we haven't yet developed. Since we want to build up all the techniques we're using by hand, then, we shouldn't permit ourselves to rely on `let rec` or `letrec` until we thoroughly understand what's going on under the hood.
+
+2.     If you tried this in Scheme:
+
+               > (define get_length 
+                               (lambda (lst) (if (null? lst) 0 (+ 1 (get_length (cdr lst))) ) ))
+
+               > (get_length (list 20 30))
+
+       You'd find that it works! This is because `define` in Scheme is really shorthand for `letrec`, not for plain `let` or `let*`. So we should regard this as cheating, too.
+
+3.     In fact, it *is* possible to define the `get_length` function in the lambda calculus despite these obstacles. This depends on using the "version 3" implementation of lists, and exploiting its internal structure: that it takes a function and a base value and returns the result of folding that function over the list, with that base value. So we could use this as a definition of `get_length`:
+
+               \lst. lst (\x sofar. successor sofar) zero
+
+       What's happening here? We start with the value zero, then we apply the function `\x sofar. successor sofar` to the two arguments <code>x<sub>n</sub></code> and `zero`, where <code>x<sub>n</sub></code> is the last element of the list. This gives us `successor zero`, or `one`. That's the value we've accumuluted "so far." Then we go apply the function `\x sofar. successor sofar` to the two arguments <code>x<sub>n-1</sub></code> and the value `one` that we've accumulates "so far." This gives us `two`. We continue until we get to the start of the list. The value we've then built up "so far" will be the length of the list.
+
+We can use similar techniques to define many recursive operations on lists and numbers. The reason we can do this is that our "version 3," fold-based implementation of lists, and Church's implementations of numbers, have a internal structure that *mirrors* the common recursive operations we'd use lists and numbers for.
+
+As we said before, it does take some ingenuity to define functions like `extract-tail` or `predecessor` for these implementations. However it can be done. (And it's not *that* difficult.) Given those functions, we can go on to define other functions like numeric equality, subtraction, and so on, just by exploiting the structure already present in our implementations of lists and numbers.
 
 With sufficient ingenuity, a great many functions can be defined in the same way. For example, the factorial function is straightforward. The function which returns the nth term in the Fibonacci series is a bit more difficult, but also achievable.
 
-However, some computable functions are just not definable in this way. The simplest function that *simply cannot* be defined using the resources we've so far developed is the Ackermann function:
+##However...##
+
+Some computable functions are just not definable in this way. The simplest function that *simply cannot* be defined using the resources we've so far developed is the Ackermann function:
 
        A(m,n) =
                | when m == 0 -> n + 1
@@ -26,9 +138,11 @@ But functions like the Ackermann function require us to develop a more general t
 
 ##How to do recursion with lower-case omega##
 
+...
+
 ##Generalizing##
 
-In general, **fixed point** of a function f is a value *x* such that f<em>x</em> is equivalent to *x*. For example, what is a fixed point of the function from natural numbers to their squares? What is a fixed point of the successor function?
+In general, **fixed point** of a function f is a value *x* such that f<em>x</em> is equivalent to *x*. For example, what is a fixed point of the function from natural numbers to their squares? What is a fixed point of the successor function?
 
 In the lambda calculus, we say a fixed point of an expression `f` is any formula `X` such that: