native `let rec` or `define`, then you can't use the fixed-point combinators
`Y` or <code>Θ</code>. Expressions using them will have non-terminating
reductions, with Scheme's eager/call-by-value strategy. There are other
- fixed-point combinators you can use with Scheme (in the [[week 3]] notes they
+ fixed-point combinators you can use with Scheme (in the [[week3]] notes they
were <code>Y′</code> and <code>Θ′</code>. But even with
- those evaluation order still matters: for some (admittedly unusual)
+ them, evaluation order still matters: for some (admittedly unusual)
evaluation strategies, expressions using them will also be non-terminating.
- The fixed-point combinators may be the conceptual heros. They are cool and
+ The fixed-point combinators may be the conceptual stars. They are cool and
mathematically elegant. But for efficiency and implementation elegance, it's
best to know how to do as much as you can without them. (Also, that knowledge
could carry over to settings where the fixed point combinators are in
This is why the v3 lists and numbers are so lovely. However, one disadvantage
to them is that it's relatively inefficient to extract a list's tail, or get a
- number's predecessor. To get the tail of the list [a;b;c;d;e], one will
+ number's predecessor. To get the tail of the list `[a;b;c;d;e]`, one will
basically be performing some operation that builds up the tail afresh: at
- different stages, one will have built up [e], then [d;e], then [c;d;e], and
- finally [b;c;d;e]. With short lists, this is no problem, but with longer lists
+ different stages, one will have built up `[e]`, then `[d;e]`, then `[c;d;e]`, and
+ finally `[b;c;d;e]`. With short lists, this is no problem, but with longer lists
it takes longer and longer. And it may waste more of your computer's memory
than you'd like. Similarly for obtaining a number's predecessor.
A clever approach would marry these two strategies.
- Version 3 makes the list [a; b; c; d; e] look like this:
+ Version 3 makes the list `[a;b;c;d;e]` look like this:
\f z. f a (f b (f c (f d (f e z))))
\f z. f a <the tail itself> <the result of folding f and z over the tail>
- That is, now f is a function expecting *three* arguments: the head of the
+ That is, now `f` is a function expecting *three* arguments: the head of the
current list, the tail of the current list, and the result of continuing to
- fold f over the tail, with a given base value z.
+ fold `f` over the tail, with a given base value `z`.
Call this a **version 4** list. The empty list could be the same:
\s z. s <pred 5> <the result of applying s to z (pred 5)-many times>
- That is, now s is a function expecting *two* arguments: the predecessor of the
- current number, and the result of continuing to apply s to the base value z
+ That is, now `s` is a function expecting *two* arguments: the predecessor of the
+ current number, and the result of continuing to apply `s` to the base value `z`
predecessor-many times.
Jim had the pleasure of "inventing" these implementations himself. However,
How might we make the implementation more efficient? Well, the *semantics* of
sets says that they have no intrinsic order. That means, there's no difference
between the set {a,b} and the set {b,a}; whereas there is a difference between
- the *list* [a;b] and the list [b;a]. But this semantic point can be respected
+ the *list* `[a;b]` and the list `[b;a]`. But this semantic point can be respected
even if we *implement* sets with something ordered, like list---as we're
already doing. And we might *exploit* the intrinsic order of lists to make our
implementation of sets more efficient.
But what if you're using v3 lists? What options would you have then for
aborting a search?
- Well, suppose we're searching through the list [5; 4; 3; 2; 1] to see if it
- contains the number 3. The expression which represents this search would have
+ Well, suppose we're searching through the list `[5;4;3;2;1]` to see if it
+ contains the number `3`. The expression which represents this search would have
something like the following form:
..................<eq? 1 3> ~~>
Of course, whether those reductions actually followed in that order would
depend on what reduction strategy was in place. But the result of folding the
- search function over the part of the list whose head is 3 and whose tail is [2;
- 1] will *semantically* depend on the result of applying that function to the
+ search function over the part of the list whose head is `3` and whose tail is `[2;
+ 1]` will *semantically* depend on the result of applying that function to the
more rightmost pieces of the list, too, regardless of what order the reduction
is computed by. Conceptually, it will be easiest if we think of the reduction
happening in the order displayed above.
- Well, once we've found a match between our sought number 3 and some member of
+ Well, once we've found a match between our sought number `3` and some member of
the list, we'd like to avoid any further unnecessary computations and just
deliver the answer `true` as "quickly" or directly as possible to the larger
computation in which the search was embedded.
But with the v3 lists, the fold is "pre-programmed" to continue over the whole
list. There is no way for us to bail out of applying the search function to the
- parts of the list that have head 4 and head 5, too.
+ parts of the list that have head `4` and head `5`, too.
- We *can* avoid some unneccessary computation. The search function can detect
+ We *can* avoid *some* unneccessary computation. The search function can detect
that the result we've accumulated so far during the fold is now true, so we
- don't need to bother comparing 4 or 5 to 3 for equality. That will simplify the
+ don't need to bother comparing `4` or `5` to `3` for equality. That will simplify the
computation to some degree, since as we said, numerical comparison in the
system we're working in is moderately expensive.
semantically, the search is the argument and the larger computation is the
function to which it's supplied.
- What if, instead, we did the same kind of thing we did with pairs and v2 lists? That is, what if we made the larger computation a "handler" that we passed as an argument to the search?
+ What if, instead, we did the same kind of thing we did with pairs and v2
+ lists? That is, what if we made the larger computation a "handler" that we
+ passed as an argument to the search?
the_search (\search_result. larger_computation search_result other_arguments)
- What's the advantage of that, you say. Other than to show off how cleverly you can lift.
+ What's the advantage of that, you say. Other than to show off how cleverly
+ you can lift.
Well, think about it. Think about the difficulty we were having aborting the
search. Does this switch-around offer us anything useful?
f 3 <result of folding f and z over [2; 1]> <handler to continue folding leftwards> <handler to abort the traversal>
- f's job would be to check whether 3 matches the element we're searching for
+ `f`'s job would be to check whether 3 matches the element we're searching for
(here also 3), and if it does, just evaluate to the result of passing `true` to
the abort handler. If it doesn't, then evaluate to the result of passing
`false` to the continue-leftwards handler.
- In this case, f wouldn't need to consult the result of folding f and z over [2;
- 1], since if we had found the element 3 in more rightward positions of the
- list, we'd have called the abort handler and this application of f to 3 etc
- would never be needed. However, in other applications the result of folding f
- and z over the more rightward parts of the list would be needed. Consider if
+ In this case, `f` wouldn't need to consult the result of folding `f` and `z` over `[2;
+ 1]`, since if we had found the element `3` in more rightward positions of the
+ list, we'd have called the abort handler and this application of `f` to `3` etc
+ would never be needed. However, in other applications the result of folding `f`
+ and `z` over the more rightward parts of the list would be needed. Consider if
you were trying to multiply all the elements of the list, and were going to
- abort (with the result 0) if you came across any element in the list that was
+ abort (with the result `0`) if you came across any element in the list that was
zero. If you didn't abort, you'd need to know what the more rightward elements
of the list multiplied to, because that would affect the answer you passed
along to the continue-leftwards handler.
A **version 5** list would encode this kind of fold operation over the list, in
the same way that v3 (and v4) lists encoded the simpler fold operation.
- Roughly, the list [5; 4; 3; 2; 1] would look like this:
+ Roughly, the list `[5;4;3;2;1]` would look like this:
\f z continue_leftwards_handler abort_handler.
Remarks: the `larger_computation_handler` should be supplied as both the
`continue_leftwards_handler` and the `abort_handler` for the leftmost
- application, where the head 5 is supplied to f. Because the result of this
+ application, where the head `5` is supplied to `f`. Because the result of this
application should be passed to the larger computation, whether it's a "fall
off the left end of the list" result or it's a "I'm finished, possibly early"
result. The `larger_computation_handler` also then gets passed to the next
- rightmost stage, where the head 4 is supplied to f, as the `abort_handler` to
+ rightmost stage, where the head `4` is supplied to `f`, as the `abort_handler` to
use if that stage decides it has an early answer.
- Finally, notice that we don't have the result of applying f to 4 etc given as
- an argument to the application of f to 5 etc. Instead, we pass
+ Finally, notice that we don't have the result of applying `f` to `4` etc given as
+ an argument to the application of `f` to `5` etc. Instead, we pass
(\result_of_fold_over_4321. f 5 result_of_fold_over_4321 one_handler another_handler)
- *to* the application of f to 4 as its "continue" handler. The application of f
- to 4 can decide whether this handler, or the other, "abort" handler, should be
+ *to* the application of `f` to `4` as its "continue" handler. The application of `f`
+ to `4` can decide whether this handler, or the other, "abort" handler, should be
given an argument and constitute its result.