Andy Elvey wrote:
> So, I may look at doing what I would call "lpred" and lsucc" - the
> predecessor and successor of a list element. I'm somewhat surprised
> that (from what I can tell) Haskell doesn't seem to have those two
> functions for a list. I may be wrong....
Again, lists don't work that way; a list in Haskell is a single
immutable object, you can pull items from it using (!!), head, etc.,
but you can't have a pointer into the "middle" of a list. You can
have a sublist (for example, `tail ["foo", "bar", "baz"]' = `["bar",
"baz"]') --- but you can't get from there to the "foo", as it isn't
part of that new list. (This despite the fact that what `tail' gives
you is going to be shared in actual storage with the original list.
You don't have a backpointer into that original list to follow.) This
is actually a feature of the Haskell model: it's usually easier to
reason about these kinds of structures, where you can treat any
operation as producing a new object and the Haskell compiler takes
care of optimizations for you (shared representations, possibly
recognizing that it can quietly do an update-in-place because you
can't possibly reach the old value, etc.).
You may want to take a look at the Seq type defined in the Data.Seq
module, though; you have a `view' (what you can think of as a cursor)
on a given Seq which can be moved back and forth.
--
brandon s. allbery [solaris,freebsd,perl,pugs,haskell]
Code:
Content visible to registered users only.
system administrator [openafs,heimdal,too many hats]
Code:
Content visible to registered users only.
electrical and computer engineering, carnegie mellon university KF8NH
_______________________________________________
Beginners mailing list
Code:
Content visible to registered users only.
Code:
Content visible to registered users only.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.10 (Darwin)
iEYEARECAAYFAkttEDIACgkQIn7hlCsL25UF+QCeLYlugNZziU wcuvDPGluB9CDt
UnQAoM0Pc8+YWzedPwhPtWg1VK0BMDm3
=C8Qj
-----END PGP SIGNATURE-----