[Haskell-cafe] STArray newListArray

Daniel Fischer daniel.is.fischer at web.de
Sun Feb 21 11:11:14 EST 2010


Am Sonntag 21 Februar 2010 16:30:00 schrieb Vojtěch Knyttl:
> Hello,
>
> I am trying to create STArray with newListArray like this:
> la x y = newListArray ((1,1),(x,y)) [(x'+y') | x' <- [1..x], y' <-
> [1..y]]
>
> – but it does not work:
> No instance for (MArray a Field m)
>
> I tried to define the type like this, but it would not work either:
> la :: Int -> Int -> STArray (Int,Int) Field

STArrays have a "state parameter",

newListArray ((1,1),(x,y)) [(x'+y') | x' <- [1 .. x], y' <- [1 .. y]]

has type 

forall s. ST s (STArray s (Int,Int) Int)

So

la :: forall s. Int -> Int -> ST s (STArray s (Int,Int) Int)
la x y = 
    newListArray ((1,1),(x,y)) [(x'+y') | x' <- [1 .. x], y' <- [1 .. y]]

You can use STArrays only in the ST monad, there you'd do something like

runST (do arr <- la 23 25
          use arr
          return result)

>
> It is obvious that I don't get the syntax of using it, so I will
> appreciate any suggestions.
>
> V. K.


More information about the Haskell-Cafe mailing list