[Haskell-cafe] Infinite grid
Luke Palmer
lrpalmer at gmail.com
Mon Dec 29 18:29:02 EST 2008
On Mon, Dec 29, 2008 at 3:55 PM, Martijn van Steenbergen <
martijn at van.steenbergen.nl> 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20081229/6242b396/attachment.htm
More information about the Haskell-Cafe
mailing list