[Haskell-cafe] How to use STArray?

Bayley, Alistair Alistair_Bayley at ldn.invesco.com
Fri Aug 26 11:22:11 EDT 2005

> From: Udo Stenzel [mailto:u.stenzel at web.de] 
> You messed the argument order to readArray up.

Yes, that won't help :-)

> Even with that repaired,
> you'll need a type signature somewhere to help ghc resolve the
> overloading of newArray and readArray, which is surprisingly 
> tricky due
> to the "s" that must not escape.  This works:
> compute :: Int -> Int
> compute n = runST ( do
>     arr <- newArray (-1, 1) n :: ST s (STArray s Int Int)
>     readArray arr 1
>   )

Thanks. This is what I needed. The thing that worked best for me was to wrap
newArray (and newArrayList) with functions that I could give signatures to.
I was hoping to keep the program as signature-free as possible, to avoid
committing heavily to particular implementations.


makeArray :: Int -> Int -> a -> ST s (STArray s Int a)
makeArray lower upper val = newArray (-lower, upper) val

makeListArray :: Int -> Int -> [a] -> ST s (STArray s Int a)
makeListArray len lst = newListArray (1, len) lst


Confidentiality Note: The information contained in this   message, and any
attachments, may contain confidential   and/or privileged material. It is
intended solely for the   person(s) or entity to which it is addressed. Any
review,   retransmission, dissemination, or taking of any action in
reliance upon this information by persons or entities other   than the
intended recipient(s) is prohibited. If you received  this in error, please
contact the sender and delete the   material from any computer.

More information about the Haskell-Cafe mailing list