[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: peasant revolt against DrScheme!
Michael Vanier wrote:
> Recursive
> functions are extremely effective for dealing with many data structures,
> lists among them. What if you want to iterate down a vector and pick out
> the largest element?
Mark Seaborn gave exactly the right answer.
When I teach programming through Scheme -- be it to high school
teachers or to graduate students -- I start by saying "We will get to
loops eventually. Obviously Scheme has loops -- what sort of language
wouldn't?" I've found this to be a great stress-reliever, especially
to the former contingent. You can see and hear the sighs of relief.
Four days/hours later, I show them MAP, FILTER and FOLD and say, "See,
now that's what a Schemer calls a loop".
Most of them achieve enlightenment.
Now Scheme doesn't have VECTOR-MAP, etc built in. Big deal. That
doesn't alter the fact that that's how you should program.
On to a larger point: I think the question is less important than you
think. Many times, when you think you want a vector, you really just
want a data structure with certain quick-access properties. For
example, a priority heap is often the right data structure.
Vector-based thinking is very often simply an artifact of your
linguistic origins. In Pascal/C, you have one built-in easy data
structure, and that is the vector. There's a simple notation for
defining and accessing them, and there is a large library built over
them. There is also a special-purpose notation for "in-lining" their
content. (Scheme does this for ALL values, as someone alluded
earlier.) So, even other data structures get implemented as vectors
-- remember that fuss your algorithms text went to to illustrate how
to use vectors to implement heaps?
Because of these benefits, you have the tendency to shoe-horn
everything into vectors. I bet if you ask most Schemers on this list,
you would find that
- their Pascal/C/... code heavily iterates over numbers
- their Scheme code rarely does, and when it does, the numbers often
have some intrinsic meaning
Many of the numbers in Pascal/C programs have no meaning. They are
simply names for points in the vector. The language has no business
imposing them on the programmer, any more than it has imposing the
addresses of list locations on the programmer. Those languages do
both. A good language would do neither. You already know Scheme does
not impose this burden for lists; Mark dramatically illustartes how to
do the same for vectors.
An unhappy side effect of this mentality is that data structures have
arbitrary a priori limits on them. These limits constitute still more
meaningless numbers in the program. And they lead to wooly-headed
thinking about interfaces. As testimony, I can produce not a few
credit cards that truncate my name ("Shriram Krishnamurthi" needs 21
characters; many appear to have a limit of 20).
Finally, you should read the section on "generative recursion" in How
to Design Programs.
Shriram