Multiple STArrays within the same block

2013-04-09

An example of creating multiple STArrays within the same runSTArray block:

{-# LANGUAGE RankNTypes #-}

import Data.Array.ST
import Control.Monad.ST

main = print $ runSTArray $ do
         xs <- newArray (1, 3::Int) (0.0::Double)
         ys <- newArray (1, 3::Int) (0.0::Double) ::  ST s (STArray s Int Double)
                                               -- ^^^ this is the trick
         flip mapM_ [1..3] $ \i -> writeArray ys i (fromIntegral i)
         flip mapM_ [1..3] $ \i -> writeArray xs i =<< readArray ys i
         return xs