
On Thu, Mar 22, 2012 at 5:45 PM, Nathan Hüsken
Hey,
I have the following problem. I have an 2D array of letters, like this:
b w y l a i l q h w r a o q e d
Now I am searching for all occurrences of a specific word in this array. The word can be horizontal, vertical or diagonal, like "bird" is in the example above. I am a beginer at haskell and I do not know where to start ...
OK, I would represent the word as an String == [Char] and my array as [[Char]] (or would some kind of fixed size array make more sense?).
In an imperative program, I would just search for the first letter and than check the rest of the word in all directions. If I do this, I need direct indexing of the array.
Any advise in which direction to think?
What about using Vector (for fast indexing and slicing) and use a 1D array? Maybe something as simple as this? import Data.Vector hiding (elem) import Prelude hiding (length) ncols :: Int ncols = 4 row :: Int -> Vector a -> Vector a row i = slice (i * ncols) ncols col :: Int -> Vector a -> Vector a col i v = let idxs = [i, i + ncols .. length v] in ifilter (\i _ -> i `elem` idxs) v diag :: Int -> Vector a -> Vector a diag i v = let idxs = [i, i + ncols + 1 .. length v] in ifilter (\i _ -> i `elem` idxs) v v :: Vector Char v = fromList "bwylailqhwraoqed" main :: IO () main = do print v print $ row 0 v print $ col 0 v print $ diag 0 v L.
Thanks! Nathan
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners