[Haskell-cafe] Trying to use more than one array in runSTUArray

Juan Miguel Vilar jvilar at uji.es
Thu Mar 15 19:27:18 CET 2012


Hello, café:

I am trying to use more than one array with runSTUArray but I don't seem
to be able to understand how it works. My first try is this:

test1 n = runSTUArray $ do
              a <- newArray (1, n) (2::Int)
              b <- newArray (1, n) (3::Int)
              forM_ [1..n] $ \i -> do
                v <- readArray a i
                writeArray b i (v+1)
              return b

but it does not work. However, when I write

test2 n = runSTUArray $ do
              let createArray v n = newArray (1, n) (v::Int)
              a <- createArray 2 n
              b <- createArray 0 n
              forM_ [1..n] $ \i -> do
                v <- readArray a i
                writeArray b i (v+1)
              return b

everything is fine although I expected the two versions to be
equivalent. To further complicate matters, the following

createArray v n = newArray (1, n) (v::Int)

test3 n = runSTUArray $ do
              a <- createArray 2 n
              b <- createArray 3 n
              forM_ [1..n] $ \i -> do
                v <- readArray a i
                writeArray b i (v+1)
              return b

does not work either. Where can I find an explanation for this
behaviour? Furthermore, what I am after is to use two arrays with
different types (Int and Bool), is it possible?

   Thanks in advance,

   Juan Miguel

-- 
Juan Miguel Vilar Torres
Profesor titular de universidad
Vicedirector de la ESTCE para ITIG e ITIS
Departamento de Lenguajes y Sistemas Informáticos
Escuela Superior de Tecnología y Ciencias Experimentales
Universitat Jaume I
Av. de Vicent Sos Baynat s/n
12071 Castelló de la Plana (Spain)
Tel: +34 964 72 8365
Fax: +34 964 72 8435
jvilar at lsi.uji.es



More information about the Haskell-Cafe mailing list