nntpnews.net

Global Usenet Archiver


Register

[Haskell-beginners] Circular Linked Lists

Reply

  #1  
Old 03-02-09, 03:45 AM
Matthew J. Williams
 
Posts: n/a
Default [Haskell-beginners] Circular Linked Lists

Dear All
How would one mimic, in Haskell, a C++ circular linked list i.e.,
where the last element precedes (points to) the first?

Sincerely
Matthew J. Williams

_______________________________________________
Beginners mailing list
[email]Beginners@haskell.org[/email]
[url]http://www.haskell.org/mailman/listinfo/beginners[/url]
Reply With Quote
  #2  
Old 03-02-09, 03:53 AM
Erik de Castro Lopo
 
Posts: n/a
Default Re: [Haskell-beginners] Circular Linked Lists

Matthew J. Williams wrote:

> How would one mimic, in Haskell, a C++ circular linked list i.e.,
> where the last element precedes (points to) the first?


Try this, "Tying the Knot":

[url]http://www.haskell.org/haskellwiki/Tying_the_Knot[/url]

Erik
--
--
-----------------------------------------------------------------
Erik de Castro Lopo
-----------------------------------------------------------------
"I consider C++ the most significant technical hazard to the survival
of your project and do so without apologies." -- Alistair Cockburn
_______________________________________________
Beginners mailing list
[email]Beginners@haskell.org[/email]
[url]http://www.haskell.org/mailman/listinfo/beginners[/url]
Reply With Quote
  #3  
Old 03-02-09, 02:46 PM
Rafael Gustavo da Cunha Pereira Pinto
 
Posts: n/a
Default Re: [Haskell-beginners] Circular Linked Lists

Matthew,

I would strongly suggest taking a look on SPJ's presentation at OSCON 2007
(video at [url]http://blip.tv/file/324976[/url]). He shows a very interesting circular
list (with a zipper).

Since you are interested in functional data structures, Chris Okasaki's book
"Purely Functional Data Structures" is a great source too!



On Tue, Feb 3, 2009 at 00:53, Erik de Castro Lopo
<mle+cl@mega-nerd.com<mle%2Bcl@mega-nerd.com>
> wrote:


> Matthew J. Williams wrote:
>
> > How would one mimic, in Haskell, a C++ circular linked list i.e.,
> > where the last element precedes (points to) the first?

>
> Try this, "Tying the Knot":
>
> [url]http://www.haskell.org/haskellwiki/Tying_the_Knot[/url]
>
> Erik
> --
> --
> -----------------------------------------------------------------
> Erik de Castro Lopo
> -----------------------------------------------------------------
> "I consider C++ the most significant technical hazard to the survival
> of your project and do so without apologies." -- Alistair Cockburn
> _______________________________________________
> Beginners mailing list
> [email]Beginners@haskell.org[/email]
> [url]http://www.haskell.org/mailman/listinfo/beginners[/url]
>




--
Rafael Gustavo da Cunha Pereira Pinto
Electronic Engineer, MSc.

_______________________________________________
Beginners mailing list
[email]Beginners@haskell.org[/email]
[url]http://www.haskell.org/mailman/listinfo/beginners[/url]

Reply With Quote
  #4  
Old 03-02-09, 02:56 PM
Dave Bayer
 
Posts: n/a
Default Re: [Haskell-beginners] Circular Linked Lists


On Feb 2, 2009, at 9:45 PM, Matthew J. Williams wrote:

> Dear All
> How would one mimic, in Haskell, a C++ circular linked list i.e.,
> where the last element precedes (points to) the first?


You are getting deep answers to what is perhaps a simple question. You
haven't said exactly what you want to do with a circular linked list,
and people are perhaps fearing the trickiest applications.

Have you tried the Prelude function cycle?

> cycle :: [a] -> [a]
> cycle ties a finite list into a circular one, or equivalently, the
> infinite repetition of the original list. It is the identity on
> infinite lists.


For instance, in ghci one gets

> Prelude> [1..3]
> [1,2,3]
> Prelude> take 10 $ cycle [1..3]
> [1,2,3,1,2,3,1,2,3,1]


cycle doesn't actually construct in memory a cyclic data structure, as
one might in C. It's more like those repeat bars in sheet music.
_______________________________________________
Beginners mailing list
[email]Beginners@haskell.org[/email]
[url]http://www.haskell.org/mailman/listinfo/beginners[/url]
Reply With Quote
  #5  
Old 03-02-09, 03:17 PM
Brent Yorgey
 
Posts: n/a
Default Re: [Haskell-beginners] Circular Linked Lists

>
> cycle doesn't actually construct in memory a cyclic data structure, as one
> might in C. It's more like those repeat bars in sheet music.


It doesn't?

cycle xs = xs' where xs' = xs ++ xs'

That sure looks like a cyclic data structure to me! xs' references a
thunk which computes (xs ++ xs'); this thunk, in turn, references
xs'. cycle is memory-efficient precisely because it *does* actually
construct a cyclic data structure.

-Brent
_______________________________________________
Beginners mailing list
[email]Beginners@haskell.org[/email]
[url]http://www.haskell.org/mailman/listinfo/beginners[/url]
Reply With Quote
  #6  
Old 03-02-09, 03:52 PM
John Hartnup
 
Posts: n/a
Default Re: [Haskell-beginners] Circular Linked Lists

2009/2/3 Brent Yorgey <byorgey@seas.upenn.edu>:

>
> cycle xs = xs' where xs' = xs ++ xs'
>
> That sure looks like a cyclic data structure to me! xs' references a
> thunk which computes (xs ++ xs'); this thunk, in turn, references
> xs'. cycle is memory-efficient precisely because it *does* actually
> construct a cyclic data structure.


That's just magnificent!



--
"There is no way to peace; peace is the way"
_______________________________________________
Beginners mailing list
[email]Beginners@haskell.org[/email]
[url]http://www.haskell.org/mailman/listinfo/beginners[/url]
Reply With Quote
  #7  
Old 03-02-09, 03:54 PM
Dave Bayer
 
Posts: n/a
Default Re: [Haskell-beginners] Circular Linked Lists

So the "repeat bars" are there until the first pass through the list
completes, otherwise cycle would be bottom on infinite lists.
Thereafter, you're saying that a core dump would reveal a completely
homogeneous memory representation, just like C code, that one could
pass through the foreign function interface to C code?

GHC seems to have a special awareness of cyclic lists. For example,
ghci computes

> (zip (cycle [1..3]) (cycle [1..4])) !! (1000^1000)


immediately, as if it knows enough to compute 1000^1000 mod 12, by
repeated squaring.

On Feb 3, 2009, at 9:17 AM, Brent Yorgey wrote:

> It doesn't?
>
> cycle xs = xs' where xs' = xs ++ xs'
>
> That sure looks like a cyclic data structure to me! xs' references a
> thunk which computes (xs ++ xs'); this thunk, in turn, references
> xs'. cycle is memory-efficient precisely because it *does* actually
> construct a cyclic data structure.


_______________________________________________
Beginners mailing list
[email]Beginners@haskell.org[/email]
[url]http://www.haskell.org/mailman/listinfo/beginners[/url]
Reply With Quote
  #8  
Old 03-02-09, 04:02 PM
Daniel Fischer
 
Posts: n/a
Default Re: [Haskell-beginners] Circular Linked Lists

Am Dienstag, 3. Februar 2009 15:54 schrieb Dave Bayer:
> So the "repeat bars" are there until the first pass through the list
> completes, otherwise cycle would be bottom on infinite lists.
> Thereafter, you're saying that a core dump would reveal a completely
> homogeneous memory representation, just like C code, that one could
> pass through the foreign function interface to C code?
>
> GHC seems to have a special awareness of cyclic lists. For example,
> ghci computes
>
> > (zip (cycle [1..3]) (cycle [1..4])) !! (1000^1000)


No, it's that the type of (!!) is [a] -> Int -> a, and 1000^1000 :: Int is 0.

>
> immediately, as if it knows enough to compute 1000^1000 mod 12, by
> repeated squaring.
>
> On Feb 3, 2009, at 9:17 AM, Brent Yorgey wrote:
> > It doesn't?
> >
> > cycle xs = xs' where xs' = xs ++ xs'
> >
> > That sure looks like a cyclic data structure to me! xs' references a
> > thunk which computes (xs ++ xs'); this thunk, in turn, references
> > xs'. cycle is memory-efficient precisely because it *does* actually
> > construct a cyclic data structure.

>


_______________________________________________
Beginners mailing list
[email]Beginners@haskell.org[/email]
[url]http://www.haskell.org/mailman/listinfo/beginners[/url]
Reply With Quote
  #9  
Old 03-02-09, 04:07 PM
Brent Yorgey
 
Posts: n/a
Default Re: [Haskell-beginners] Circular Linked Lists

On Tue, Feb 03, 2009 at 09:54:46AM -0500, Dave Bayer wrote:
> So the "repeat bars" are there until the first pass through the list
> completes, otherwise cycle would be bottom on infinite lists. Thereafter,
> you're saying that a core dump would reveal a completely homogeneous memory
> representation, just like C code, that one could pass through the foreign
> function interface to C code?


I'm not really sure what you mean by "repeat bars". There really is a
cyclic data structure in memory at all times--it's just that until the
first pass through the list, part of it is a thunk. After a complete
pass to the list, however, a core dump would indeed reveal something
like what you suggest.

>
> GHC seems to have a special awareness of cyclic lists. For example, ghci
> computes
>
>> (zip (cycle [1..3]) (cycle [1..4])) !! (1000^1000)

>
> immediately, as if it knows enough to compute 1000^1000 mod 12, by repeated
> squaring.


I came to the same conclusion as Daniel but it took me a few minutes
of puzzlement. Besides, it should actually be equal to (2,1), not
(1,1). =)

-Brent
_______________________________________________
Beginners mailing list
[email]Beginners@haskell.org[/email]
[url]http://www.haskell.org/mailman/listinfo/beginners[/url]
Reply With Quote
  #10  
Old 03-02-09, 04:15 PM
Dave Bayer
 
Posts: n/a
Default Re: [Haskell-beginners] Circular Linked Lists


On Feb 3, 2009, at 10:04 AM, Daniel Fischer wrote:

>> GHC seems to have a special awareness of cyclic lists. For example,
>> ghci computes
>>
>>> (zip (cycle [1..3]) (cycle [1..4])) !! (1000^1000)

>
> No, it's that the type of (!!) is [a] -> Int -> a, and 1000^1000 ::
> Int is 0.
>
>>
>> immediately, as if it knows enough to compute 1000^1000 mod 12, by
>> repeated squaring.


Thanks.

The following takes forever, but it doesn't consume memory:

> Prelude> :m Data.List
> Prelude Data.List> genericIndex (zip (cycle [1..3]) (cycle [1..4]))
> (1000^1000)


So zip is doing something smart here with cyclic lists.

_______________________________________________
Beginners mailing list
[email]Beginners@haskell.org[/email]
[url]http://www.haskell.org/mailman/listinfo/beginners[/url]
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] Circular programming Maciej Piechotka fa.haskell 3 16-08-09 12:14 PM
[Haskell-beginners] Updating lists inside another list Aaron MacDonald fa.haskell 2 23-06-09 09:19 AM
[Haskell-beginners] Searching Maybe lists aditya siram fa.haskell 12 23-05-09 12:33 PM
[Haskell-beginners] Question on Lists Nathan Holden fa.haskell 1 19-04-09 10:44 PM
[Haskell-beginners] linked lists Matthew J. Williams fa.haskell 1 03-02-09 03:15 AM


All times are GMT +1. The time now is 05:48 PM. 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