Matthew J. Williams | 3 Feb 2009 03:01

linked lists

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
David Frey | 3 Feb 2009 03:15
Picon
Favicon

Re: linked lists

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?
Matthew J. Williams | 3 Feb 2009 03:45

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
Erik de Castro Lopo | 3 Feb 2009 03:53
Favicon

Re: 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":

    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
Picon

Re: Circular Linked Lists

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:

> 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
_______________________________________________
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
Dave Bayer | 3 Feb 2009 14:56

Re: 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.
Brent Yorgey | 3 Feb 2009 15:17
Favicon
Gravatar

Re: 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
John Hartnup | 3 Feb 2009 15:52
Picon

Re: Circular Linked Lists

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"
Dave Bayer | 3 Feb 2009 15:54

Re: 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.
Daniel Fischer | 3 Feb 2009 16:04
Picon

Re: 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.
>

Gmane