[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: functional programming is great, but why lists?



You *could* treat lists the way you do.  Sure, there are some examples 
for which is't pretty nice -- it removes the asymmetry of FIRST/REST
by providing a uniform way in which to write what some would think
should be similar procedures (you picked the canonical pair of
examples).  But I think that, for the most part, you *don't* want to
program with indices.  They often (especially in the wrong hands)
reflect the inability of a programmer (not necessarily you) to treat
values as values, referring them through surrogate names instead.
Indeed, teaching lists this way obscures their value-ness.

There's a good reason why lists don't naturally come with indices the
way vectors do: they're not intended to be accessed at random.  They
really are intended to be accessed sequentially.  That's their cost
model.  So what you really want to do is to define an abstract data
type, call it Indexed, and create primitives that consume and return
Indexed elements.  You would probably implement Indexed as an
immutable vector.  Once you provide MAP, FILTER and FOLD over Indexed
values, there'd be no need to define INDEXED-FIRST and INDEXED-REST.

Shriram