I have array of x,y coordinates and char elements. I need function to get list of n chars at specified coordinates toward direction. I wrote it in recurrent way:
import Data.Array
exampleArray = array ((1,1),(2,2)) [((1,1),'a'), ((1,2),'b'), ((2,1),'c'), ((2,2),'d')]
-- 1 2
-- +---+---+
-- 1 | a | b |
-- +---+---+
-- 2 | c | d |
-- +---+---+
f :: Array (Int, Int) Char -> Int -> (Int, Int) -> (Int, Int) -> [Char]
f _ 0 _ _ = []
f arr n (x,y) (dirX, dirY) = (arr ! (y,x)) : f arr (n-1) (x+dirX,y+dirY) (dirX, dirY)
-- ac
list = f exampleArray 2 (1,1) (0,1) -- get 2 chars from array at 1,1 toward E
-- cb
list2 = f exampleArray 2 (1,2) (1,(-1)) -- get 2 chars from array at 1,2 toward NE
It works, but it is too slow and I am wondering how to optimize it?