Array and Ix for custom Graph ADT

Hi, I'm creating a Graph data structure, and I want to use the array list approach, implemented as an Array. I need my Nodes to be instances of Ix for this to work and my Node type is roughly as follows: data Node = MyNode Int [Int] Type1 Type2 (Type1 and Type2 are nullary algebraic datatypes--enumerations in other words) How can I "index" the Array with an instance of a Node. I think I need to make Node an instance of Ix somehow?? Is this a sensible approach? Sample code of the instance declaration for Ix would be helpful. Thanks Mark Spezzano

On Sat, Aug 13, 2011 at 4:37 AM, Mark Spezzano
Hi,
I'm creating a Graph data structure, and I want to use the array list approach, implemented as an Array.
I need my Nodes to be instances of Ix for this to work and my Node type is roughly as follows:
data Node = MyNode Int [Int] Type1 Type2
(Type1 and Type2 are nullary algebraic datatypes--enumerations in other words)
How can I "index" the Array with an instance of a Node. I think I need to make Node an instance of Ix somehow??
What do the 'Int' and '[Int]' fields represent in your Node type? Antoine
Is this a sensible approach? Sample code of the instance declaration for Ix would be helpful.
Thanks
Mark Spezzano
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Hi Antoine, The first Int field is a unique index, beginning at 1 and increasing by 1 for each unique Node. The second [Int] field is just a list of random numbers associated with that node. Mark On 14/08/2011, at 2:13 AM, Antoine Latter wrote:
On Sat, Aug 13, 2011 at 4:37 AM, Mark Spezzano
wrote: Hi,
I'm creating a Graph data structure, and I want to use the array list approach, implemented as an Array.
I need my Nodes to be instances of Ix for this to work and my Node type is roughly as follows:
data Node = MyNode Int [Int] Type1 Type2
(Type1 and Type2 are nullary algebraic datatypes--enumerations in other words)
How can I "index" the Array with an instance of a Node. I think I need to make Node an instance of Ix somehow??
What do the 'Int' and '[Int]' fields represent in your Node type?
Antoine
Is this a sensible approach? Sample code of the instance declaration for Ix would be helpful.
Thanks
Mark Spezzano
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Sunday 14 August 2011, 00:41:33, Mark Spezzano wrote:
Hi Antoine,
The first Int field is a unique index, beginning at 1 and increasing by 1 for each unique Node.
Then you could use that for indexing, assuming the Ord instance matches. range (MyNode x _ _ _, MyNode y _ _ _) = [x .. y] index (MyNode x _ _ _, MyNode y _ _ _) (MyNode z _ _ _) | x <= z && z <= y = z-x | otherwise = error ("Index out of range: " ++ show z) etc.
The second [Int] field is just a list of random numbers associated with that node.
Mark

Solved it with your help :) Thanks for your assistance! Mark Spezzano On 14/08/2011, at 8:29 AM, Daniel Fischer wrote:
On Sunday 14 August 2011, 00:41:33, Mark Spezzano wrote:
Hi Antoine,
The first Int field is a unique index, beginning at 1 and increasing by 1 for each unique Node.
Then you could use that for indexing, assuming the Ord instance matches.
range (MyNode x _ _ _, MyNode y _ _ _) = [x .. y] index (MyNode x _ _ _, MyNode y _ _ _) (MyNode z _ _ _) | x <= z && z <= y = z-x | otherwise = error ("Index out of range: " ++ show z)
etc.
The second [Int] field is just a list of random numbers associated with that node.
Mark
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (3)
-
Antoine Latter
-
Daniel Fischer
-
Mark Spezzano