On Mon, Dec 29, 2008 at 3:55 PM, Martijn van Steenbergen <
[email protected]> wrote:

> Hello,
>
> I would like to construct an infinite two-dimensional grid of nodes, where
> a node looks like this:
>
>  data Node = Node
>>  { north :: Node
>>  , east  :: Node
>>  , south :: Node
>>  , west  :: Node
>>  }
>>
>
> in such a way that for every node n in the grid it doesn't matter how I
> travel to n, I always end up in the same memory location for that node.


No problem:

let n = Node n n n n in n

But you probably want some additional data in each node, also, in which the
problem becomes harder.

Here is one quarter of it (it only extends to the north and east of the root
node), you can fill in the rest.  I put in Debug.Trace so we can observe
that they are actually shared.

import Debug.Trace

grid = nodes !! 0 !! 0
  where
  minus1 0 = 0
  minus1 n = n-1
  node i j = Node { north = nodes !! i !! (j+1)
                  , east  = nodes !! (i+1) !! j
                  , south = nodes !! i !! minus1 j
                  , west  = nodes !! minus1 i !! j
                  , contents = trace "Compute!" (i,j)
                  }
  nodes = [ [ node i j | j <- [0..] ] | i <- [0..] ]

Luke
_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to