X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=blobdiff_plain;f=week3.mdwn;h=29a058c04350347ac05aa7ef32087fb09020cbdc;hp=1f1b63250ea140daf6d21aff9505f94d643f6f44;hb=113625cac7cb6fe962092be8fb92770dc90756f3;hpb=cddaaa939b1b1af89dea38afa0d9e71f008050f7 diff --git a/week3.mdwn b/week3.mdwn index 1f1b6325..29a058c0 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: @@ -23,7 +24,7 @@ Some comments on this: 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: +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`. So our recursively-defined function works the way we'd expect it to. In OCaml: let rec get_length = fun lst -> if lst == [] then 0 else 1 + get_length (tail lst)