X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=blobdiff_plain;f=topics%2Fweek1.mdwn;h=446d7a724fc609ec45a7b253c1505873d92ecc93;hp=4e1f0ddaca7f39968d1adfc60956289968cbeaf1;hb=d287d7719b3b2b0418e8b00b7fd2ec306fd3ec5a;hpb=79cec6a35353e38753e06837b4613b729dce9e8c
diff --git a/topics/week1.mdwn b/topics/week1.mdwn
index 4e1f0dda..446d7a72 100644
--- a/topics/week1.mdwn
+++ b/topics/week1.mdwn
@@ -81,7 +81,7 @@ I've started throwing in some **variables**. We'll say variables are any express
x?
xs
-We'll follow a *convention* of using variables with short names and a final `s` to represent collections like sequences (to be discussed below). But this is just a convention to help us remember what we're up to, not a strict rule of the language. We'll also follow a convention of only using variables ending in `?` to represent functions that return a boolean value. Thus, for example, `zero?` will be a function that expects a single number argument and returns a boolean corresponding to whether that number is `0`. `odd?` will be a function that expects a single number argument and returns a boolean corresponding to whether than number is odd. Above, I suggested we might use `lessthan?` to represent a function that expects *two* number arguments, and again returns a boolean result.
+We'll follow a *convention* of using variables with short names and a final `s` to represent collections like sequences (to be discussed below). But this is just a convention to help us remember what we're up to, not a strict rule of the language. We'll also follow a convention of only using variables ending in `?` to represent functions that return a boolean value. Thus, for example, `zero?` will be a function that expects a single number argument and returns a boolean corresponding to whether that number is `0`. `odd?` will be a function that expects a single number argument and returns a boolean corresponding to whether than number is odd. Above, I suggested we might use `lessthan?` to represent a function that expects *two* number arguments, and again returns a boolean result.
We also conventionally reserve variables ending in `!` for a different special class of functions, that we will explain later in the course.
@@ -537,7 +537,7 @@ As we said, this is deep and exciting, and it will make your head spin before we
Finally, we're in a position to revisit the two definitions of `length` that Jim presented in class. Here is the first:
`letrec`
- `length match` λ `xs. case xs of [] then 0; _:ys then 1 + length ys end`
+ `length match` λ `xs. case xs of [] then 0; _ & ys then 1 + length ys end`
`in length`
This function accept a sequence `xs`, and if it's empty returns `0`, else it says that its length is `1` plus whatever is the length of its remainder when you take away the first element. In programming circles, this remainder is commonly called the sequence's "tail" (and the first element is its "head").
@@ -547,7 +547,7 @@ Thus if we evaluated `length [10, 20, 30]`, that would give the same result as `
Here's another way to define the `length` function:
`letrec`
- `aux match` λ `(n, xs). case xs of [] then n; _:ys then aux (n + 1, ys) end`
+ `aux match` λ `(n, xs). case xs of [] then n; _ & ys then aux (n + 1, ys) end`
`in` λ `xs. aux (0, xs)`
This may be a bit confusing. What we have here is a helper function `aux` (for "auxiliary") that accepts *two* arguments, the first being a counter of how long we've counted in the sequence so far, and the second argument being how much more of the sequence we have to inspect. If the sequence we have to inspect is empty, then we're finished and we can just return our counter. (Note that we don't return `0`.) If not, then we add `1` to the counter, and proceed to inspect the tail of the sequence, ignoring the sequence's first element. After the `in`, we can't just return the `aux` function, because it expects two arguments, whereas `length` should just be a function of a single argument, the sequence whose length we're inquiring about. What we do instead is return a λ-generated function, that expects a single sequence argument `xs`, and then returns the result of calling `aux` with that sequence together with an initial counter of `0`.