runST example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
module Test where

import qualified Data.Vector.Unboxed.Mutable as MV
import Control.Monad
import Control.Monad.ST
import qualified Data.Vector.Unboxed as UV

testFun :: UV.Vector Int
testFun =  
   runST $ do
     d <- MV.replicate 4 0
     forM_ [0..3] $ \i -> 
       forM_ [0,2] $ \j ->
         when (i + j < 3) $ do
           value <- MV.read d (i+j) 
           MV.write d (i+j) (value + 1)
     UV.unsafeFreeze d