-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.)

+2. `cdr` is function that gets the tail of a Scheme list. (By definition, it's the function for getting the second member of a [[dotted pair|week3_unit#imp]]. As we discussed in notes for last week, it just turns out to return the tail of a list because of the particular way Scheme implements lists.)

-3. I use `length` instead of the convention we've been following so far of hyphenated names, as in `make-list`, because we're discussing OCaml code here, too, and OCaml doesn't permit the hyphenated variable names. OCaml requires variables to always start with a lower-case letter (or `_`), and then continue with only letters, numbers, `_` or `'`. Most other programming languages are similar. Scheme is very relaxed, and permits you to use `-`, `?`, `/`, and all sorts of other crazy characters in your variable names.

-

-4. I alternate between `[ ]`s and `( )`s in the Scheme code just to make it more readable. These have no syntactic difference.

+3. We alternate between `[ ]`s and `( )`s in the Scheme code just to make it more readable. These have no syntactic difference.

The main question for us to dwell on here is: What are the `let rec` in the OCaml code and the `letrec` in the Scheme code?

-Answer: These work like the `let` expressions we've already seen, except that they let you use the variable `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 `length`. So our recursively-defined function works the way we'd expect it to. In OCaml:

+Answer: These work a lot like `let` expressions, except that they let you use the variable `length` *inside* the body of the function being bound to it---with the understanding that it will there be bound to *the same function* that you're *then* in the process of binding `length` to. So our recursively-defined function works the way we'd expect it to. Here is OCaml:

- let rec length = fun lst ->

- if lst == [] then 0 else 1 + length (tail lst)

- in length [20; 30]

- (* this evaluates to 2 *)

+ let rec length = fun xs ->

+ if xs == [] then 0 else 1 + length (tail xs)

+ in length [20; 30]

+ (* this evaluates to 2 *)

-In Scheme:

+Here is Scheme:

- (letrec [(length

- (lambda (lst) (if (null? lst) 0 [+ 1 (length (cdr lst))] )) )]

- (length (list 20 30)))

- ; this evaluates to 2

+ (letrec [(length (lambda (xs)

+ (if (null? xs) 0

+ (+ 1 (length (cdr xs))) )))]

+ (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 length = fun lst ->

- if lst == [] then 0 else 1 + length (tail lst)

- in length [20; 30]

- (* fails with error "Unbound value length" *)

+ let length = fun xs ->

+ if xs == [] then 0 else 1 + length (tail xs)

+ in length [20; 30]

+ (* fails with error "Unbound value length" *)

Here's Scheme:

- (let* [(length

- (lambda (lst) (if (null? lst) 0 [+ 1 (length (cdr lst))] )) )]

- (length (list 20 30)))

- ; fails with error "reference to undefined identifier: length"

+ (let* [(length (lambda (xs)

+ (if (null? xs) 0

+ (+ 1 (length (cdr xs))) )))]

+ (length (list 20 30)))

+ ; fails with error "reference to undefined identifier: length"

Why? Because we said that constructions of this form:

- let length = A

- in B

+ let

+ length match/= A

+ in B

really were just another way of saying:

- (\length. B) A

+ (\length. B) A

and so the occurrences of `length` in A *aren't bound by the `\length` that wraps B*. Those occurrences are free.

-We can verify this by wrapping the whole expression in a more outer binding of `length` to some other function, say the constant function from any list to the integer 99:

+We can verify this by wrapping the whole expression in a more outer binding of `length` to some other function, say the constant function from any list to the integer `99`:

- let length = fun lst -> 99

- in let length = fun lst ->

- if lst == [] then 0 else 1 + length (tail lst)

- in length [20; 30]

- (* evaluates to 1 + 99 *)

+ let length = fun xs -> 99

+ in let length = fun xs ->

+ if xs == [] then 0 else 1 + length (tail xs)

+ in length [20; 30]

+ (* evaluates to 1 + 99 *)

-Here the use of `length` in `1 + length (tail lst)` can clearly be seen to be bound by the outermost `let`.

+Here the use of `length` in `1 + length (tail xs)` can clearly be seen to be bound by the outermost `let`.

-And indeed, if you tried to define `length` in the lambda calculus, how would you do it?

+And indeed, if you tried to define `length` in the Lambda Calculus, how would you do it?

- \lst. (isempty lst) zero (add one (length (extract-tail lst)))

+ \xs. (empty? xs) 0 (succ (length (tail xs)))

-We've defined all of `isempty`, `zero`, `add`, `one`, and `extract-tail` in earlier discussion. But what about `length`? That's not yet defined! In fact, that's the very formula we're trying here to specify.

+We've defined all of `empty?`, `0`, `succ`, and `tail` in earlier discussion. But what about `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)))

+ \xs. (empty? xs) 0 (succ (... (tail xs)))

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)))

+ \xs. (empty? xs) 0 (succ (\xs. (empty? xs) 0 (succ (... (tail xs)))

+ (tail xs)))

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:

+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.

- (define length

- (lambda (lst) (if (null? lst) 0 [+ 1 (length (cdr lst))] )) )

+2. If you tried this in Scheme:

- (length (list 20 30))

+ (define length (lambda (xs)

+ (if (null? xs) 0

+ (+ 1 (length (cdr xs))) )))

+

+ (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.

+ 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 `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 `length`:

+3. In fact, it *is* possible to define the `length` function in the Lambda Calculus despite these obstacles. This depends on using the "version 3" encoding 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 `length`:

- \lst. lst (\x sofar. successor sofar) zero

+ \xs. xs (\x sofar. successor sofar) 0

- 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 accumulated "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.

+ What's happening here? We start with the value `0`, then we apply the function `\x sofar. successor sofar` to the two arguments <code>x<sub>n</sub></code> and `0`, where <code>x<sub>n</sub></code> is the last element of the list. This gives us `successor 0`, or `1`. 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 `1` that we've accumulated "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

+fold-based encoding of lists, and Church's encodings of

numbers, have a internal structure that *mirrors* the common recursive

operations we'd use lists and numbers for. In a sense, the recursive

structure of the `length` operation is built into the data

@@ -146,7+148,7 @@ This is one of the themes of the course: using data structures to

encode the state of some recursive operation. See discussions of the

[[zipper]] technique, and [[defunctionalization]].

-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.

+As we said before, it does take some ingenuity to define functions like `tail` or `predecessor` for these encodings. 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 encodings 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.

to some list `L`, we're not going to go into an infinite evaluation loop of that sort. At each cycle, we're going to be evaluating the application of:

- \lst. (isempty lst) zero (add one (self (extract-tail lst)))

+ \xs. (empty? xs) 0 (succ (self (tail xs)))

-to *the tail* of the list we were evaluating its application to at the previous stage. Assuming our lists are finite (and the implementations we're using don't permit otherwise), at some point one will get a list whose tail is empty, and then the evaluation of that formula to that tail will return `zero`. So the recursion eventually bottoms out in a base value.

+to *the tail* of the list we were evaluating its application to at the previous stage. Assuming our lists are finite (and the encodings we're using don't permit otherwise), at some point one will get a list whose tail is empty, and then the evaluation of that formula to that tail will return `0`. So the recursion eventually bottoms out in a base value.

##Fixed-point Combinators Are a Bit Intoxicating##

@@ -504,11+506,11 @@ I used <code>&Psi;</code> above to stand in for an arbitrary fixed-point combina

As we said, there are many other fixed-point combinators as well. For example, Jan Willem Klop pointed out that if we define `L` to be: