I wrote small program in haskell to count all ocurences of Int values in Tree using State Monad with Vector:
import Data.Vector
import Control.Monad.State
import
When using ST
state you're never explicitly passing the vector around (that's always in hidden in the s
argument), but a reference to it. That reference is immutable and not copied, so you need not State
but simply a reader to pass it implicitly.
import Data.Vector
import Control.Monad.Reader
import qualified Data.Vector.Mutable as VM
import Control.Monad.ST
type MyMon3 s = ReaderT (VM.MVector s Int) (ST s)
data Tree a = Null | Node (Tree a) a (Tree a) deriving Show
main :: IO ()
main = do
print $ runTraverse (Node Null 5 Null)
runTraverse :: Tree Int -> Vector Int
runTraverse t = runST $ do
emp <- VM.replicate 7 0
runReaderT (traverse t) emp
Data.Vector.freeze emp
traverse :: Tree Int -> MyMon3 s ()
traverse Null = return ()
traverse (Node l v r) = do
d <- ask
a <- lift $ VM.read d v
lift $ VM.write d v (a + 1)