3 Feb 2009 03:01
3 Feb 2009 03:15
Re: linked lists
David Frey <dpfrey <at> shaw.ca>
2009-02-03 02:15:27 GMT
2009-02-03 02:15:27 GMT
On Tue, 03 Feb 2009 02:01:33 +0000, "Matthew J. Williams" <matthewjwilliams1 <at> googlemail.com> wrote: > Dear All > What would be the haskell equivalent of the C++ linked list complete > with nodes? the list structure -- x:[} -- is the closest I've found. > > Sincerely > Matthew J. Williams > There are certainly some similarities between std::list from C++ and Haskell's list, but there are also a lot of differences partly because C++ and Haskell are very different languages. What properties of lists are you interested in comparing?
3 Feb 2009 03:45
3 Feb 2009 03:53
Re: Circular Linked Lists
Erik de Castro Lopo <mle+cl <at> mega-nerd.com>
2009-02-03 02:53:17 GMT
2009-02-03 02:53:17 GMT
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":
http://www.haskell.org/haskellwiki/Tying_the_Knot
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
3 Feb 2009 14:46
Re: Circular Linked Lists
Rafael Gustavo da Cunha Pereira Pinto <RafaelGCPP.Linux <at> gmail.com>
2009-02-03 13:46:06 GMT
2009-02-03 13:46:06 GMT
Matthew,
I would strongly suggest taking a look on SPJ's presentation at OSCON 2007 (video at http://blip.tv/file/324976). 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 <at> mega-nerd.com> wrote:
Matthew J. Williams wrote:Try this, "Tying the Knot":
> How would one mimic, in Haskell, a C++ circular linked list i.e.,
> where the last element precedes (points to) the first?
http://www.haskell.org/haskellwiki/Tying_the_Knot
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
Beginners <at> haskell.org
http://www.haskell.org/mailman/listinfo/beginners
--
Rafael Gustavo da Cunha Pereira Pinto
Electronic Engineer, MSc.
_______________________________________________ Beginners mailing list Beginners <at> haskell.org http://www.haskell.org/mailman/listinfo/beginners
3 Feb 2009 14:56
Re: Circular Linked Lists
Dave Bayer <bayer <at> cpw.math.columbia.edu>
2009-02-03 13:56:34 GMT
2009-02-03 13:56:34 GMT
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.
3 Feb 2009 15:17
Re: Circular Linked Lists
Brent Yorgey <byorgey <at> seas.upenn.edu>
2009-02-03 14:17:12 GMT
2009-02-03 14:17:12 GMT
> > 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
3 Feb 2009 15:52
Re: Circular Linked Lists
John Hartnup <john.hartnup <at> gmail.com>
2009-02-03 14:52:33 GMT
2009-02-03 14:52:33 GMT
2009/2/3 Brent Yorgey <byorgey <at> 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"
3 Feb 2009 15:54
Re: Circular Linked Lists
Dave Bayer <bayer <at> cpw.math.columbia.edu>
2009-02-03 14:54:46 GMT
2009-02-03 14:54:46 GMT
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.
3 Feb 2009 16:04
Re: Circular Linked Lists
Daniel Fischer <daniel.is.fischer <at> web.de>
2009-02-03 15:04:11 GMT
2009-02-03 15:04:11 GMT
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. >
RSS Feed