nntpnews.net

Global Usenet Archiver


Register

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

Reply

  #1  
Old 06-02-10, 06:46 AM
Brandon S. Allbery KF8NH
 
Posts: n/a
Default Re: [Haskell-beginners] Just clarifying the "pred" and "succ"functions in Haskell

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-----

Reply With Quote
  #2  
Old 06-02-10, 11:02 AM
Felipe Lessa
 
Posts: n/a
Default Re: [Haskell-beginners] Just clarifying the "pred" and "succ"functions in Haskell

On Sat, Feb 06, 2010 at 01:45:58AM -0500, Brandon S. Allbery KF8NH wrote:
> 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.)


Although we can't have a pointer to the middle of a list [a], we
can have another data struture that behaves sort of as if we had
that pointer: a zipper. For example,

Code:
Content visible to registered users only.
To understand a list zipper you could think of it as something like

struct zipper {
list left, right;
}

So we track our "position" by tracking what's on the left and
what's on the right. By using the API of the link above you
could have something similar to what you wanted:

lpred :: Zipper a -> a
lpred = focus . left

lsucc :: Zipper a -> a
lsucc = focus . right

--
Felipe.
_______________________________________________
Beginners mailing list
Code:
Content visible to registered users only.
Code:
Content visible to registered users only.
Reply With Quote
  #3  
Old 06-02-10, 08:45 PM
Andy Elvey
 
Posts: n/a
Default Re: [Haskell-beginners] Just clarifying the "pred" and "succ"functions in Haskell

Felipe Lessa wrote:

(snip )
>
> Although we can't have a pointer to the middle of a list [a], we
> can have another data struture that behaves sort of as if we had
> that pointer: a zipper. For example,
>
>
Code:
Content visible to registered users only.
>
> To understand a list zipper you could think of it as something like
>
> struct zipper {
> list left, right;
> }
>
> So we track our "position" by tracking what's on the left and
> what's on the right. By using the API of the link above you
> could have something similar to what you wanted:
>
> lpred :: Zipper a -> a
> lpred = focus . left
>
> lsucc :: Zipper a -> a
> lsucc = focus . right
>
> --
> Felipe.
>

Hi Felipe -
Thanks very much for that! That's very handy to know. I think that
I'll base my lpred and lsucc on this approach.
Bye for now -
- Andy

_______________________________________________
Beginners mailing list
Code:
Content visible to registered users only.
Code:
Content visible to registered users only.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Haskell-beginners] Just clarifying the "pred" and "succ" functionsin Haskell Andy Elvey fa.haskell 2 06-02-10 06:32 AM
[Haskell-beginners] "reusable" data in Haskell John Velman fa.haskell 2 04-12-09 04:34 PM
[Haskell-beginners] Help with "20 intermediate haskell exercises" Patrick LeBoutillier fa.haskell 6 06-07-09 10:10 PM
[Haskell-beginners] What is the Haskell idiom for "if then else ifthen" Peter Hickman fa.haskell 3 31-03-09 09:03 AM
[Haskell-cafe] "non-functions like unsafePerformIO are nottechnically part of the haskell language!" Jason Dusek fa.haskell 4 20-12-08 04:42 PM


All times are GMT +1. The time now is 09:14 AM. Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0



For ads on this site use independent advertising companies. These companies may use some data (which does not include your name, address, email address or telephone number) about your visits to this and other websites to create advertisements on products and services you might enjoy. If you'd like more information and to know the options available to prevent the use of such information by these companies, click here

Abuse Ticket System