Search code examples
arrayshaskellstate-monad

Haskell: How to combine scalar and monadic values?


Currently I'm fiddling around with a haskell problem concerning an 2D ST Array and recursion.

Given a 2D position and an array of directions, I wrote a line returning a list of all resulting points inside the array:

let cellsAround = [resPt | dir <- directions,
                         let resPt = (fst dir + fst point, snd dir + snd point),
                         fst resPt >= 0 && fst resPt <= fst maxIdx &&
                         snd resPt >= 0 && snd resPt <= snd maxIdx]

Now the goal is to enrich the resulting list items with the contents of the array and I tried this one:

cellsAround <- sequence [readArray board resPt | dir <- directions,
                         let resPt = (fst dir + fst point, snd dir + snd point),
                         fst resPt >= 0 && fst resPt <= fst maxIdx &&
                         snd resPt >= 0 && snd resPt <= snd maxIdx]

This also works great. But the goal is to get a combination of both [(Point, Int)] because I have to filter on the array content.

Any ideas how to combine this to, say

(resPt, readArray board resPt)

?


Solution

  • A minimal change:

    sequence [(,) resPt <$> readArray board resPt
             | ...