[Haskell-beginners] Just clarifying the "pred" and "succ" functions in Haskell

Brandon S. Allbery KF8NH allbery at ece.cmu.edu
Sat Feb 6 01:45:58 EST 2010

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] allbery at kf8nh.com
system administrator [openafs,heimdal,too many hats] allbery at ece.cmu.edu
electrical and computer engineering, carnegie mellon university    KF8NH

-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 195 bytes
Desc: This is a digitally signed message part
Url : http://www.haskell.org/pipermail/beginners/attachments/20100206/e705b3a9/PGP.bin

More information about the Beginners mailing list