On Mon, Dec 29, 2008 at 3:55 PM, Martijn van Steenbergen <span dir="ltr"><<a href="mailto:martijn@van.steenbergen.nl">martijn@van.steenbergen.nl</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hello,<br>
<br>
I would like to construct an infinite two-dimensional grid of nodes, where a node looks like this:<br>
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
data Node = Node<br>
{ north :: Node<br>
, east :: Node<br>
, south :: Node<br>
, west :: Node<br>
}<br>
</blockquote>
<br>
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.</blockquote><div><br>No problem:<br><br style="font-family: courier new,monospace;">
<font style="font-family: courier new,monospace;" face="comic sans ms,sans-serif">let n = Node n n n n in n</font><br><br>But you probably want some additional data in each node, also, in which the problem becomes harder.<br>
<br>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.<br><br><font face="courier new,monospace">import Debug.Trace<br>
<br>grid = nodes !! 0 !! 0 <br> where<br> minus1 0 = 0<br> minus1 n = n-1<br> node i j = Node { north = nodes !! i !! (j+1)<br> , east = nodes !! (i+1) !! j<br> , south = nodes !! i !! minus1 j<br>
, west = nodes !! minus1 i !! j<br> , contents = trace "Compute!" (i,j)<br> }<br> nodes = [ [ node i j | j <- [0..] ] | i <- [0..] ]<br></font> <br>Luke<br>
</div></div><br>