X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=blobdiff_plain;f=week3.mdwn;h=f5f075a170c5ba16901b5b2cfb9445fea3ca1b4d;hp=07a14f5bd07c8fb64ccea2cf871660c73b6b082d;hb=bdb2d785e628954f3237010069716818db51a2b3;hpb=310d9657adff1a6e0a331656f08c53dd9d3c3b5e diff --git a/week3.mdwn b/week3.mdwn index 07a14f5b..f5f075a1 100644 --- a/week3.mdwn +++ b/week3.mdwn @@ -2,8 +2,9 @@ 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: - the empty list has length 0 - any non-empty list has length 1 + (the length of its tail) +> 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: @@ -19,8 +20,9 @@ In Scheme you'd define it like this: 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.) +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: