I'm trying to implement a function to match a specific pattern in Haskell inside a 2D grid. For example, if my grid is defined as follows:
grid = [[0,0,0,0],
[0,1,1,0],
[0,1,1,0],
[0,0,0,0]]
and the pattern I am looking for is:
pattern = [[1, 1],
[0, 0]]
I would like it to return the index of the top left element in the grid. In this case, (2,1)
. My function has the following type:
matches2d :: Eq a
=> [[a]] -- ^ The 2D grid to match over
-> [[a]] -- ^ The 2D pattern to match
-> [(Int, Int)] -- ^ Top left corner of sections matching the pattern
I am not exactly sure how to approach this problem. The method I am trying to use here is a sliding window of the pattern over the grid, but I've been trying to implement something like that for a bit now. This is what I have so far, but I think I'm missing some of the logic behind it:
matches2d :: Eq a => [[a]] -> [[a]] -> [(Int, Int)]
matches2d _ [] = []
matches2d f (x:xs) = matcher x pattern
where matcher x y (x:xs) = sort x == sort y
Thanks in advance for you help.