RE: [Haskell-cafe] How to use STArray?

From: Udo Stenzel [mailto:u.stenzel@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. e.g. 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 Alistair. ----------------------------------------- ***************************************************************** 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. *****************************************************************

On Fri, Aug 26, 2005 at 04:22:11PM +0100, Bayley, Alistair wrote:
makeArray :: Int -> Int -> a -> ST s (STArray s Int a) makeArray lower upper val = newArray (-lower, upper) val ^ Is negation of lower intentional here?
Best regards Tomasz

makeArray :: Int -> Int -> a -> ST s (STArray s Int a) makeArray lower upper val = newArray (-lower, upper) val ^ Is negation of lower intentional here?
Yes, 'tis. Arguable as to whether or not it's a good idea, as it changes the inerface. In my program I use two types of array: - indexed from 1 to n - indexed from a -ve lower to a +ve upper So in this case it made sense to just pass the lower bound and negate it in the wrapper.
participants (3)
-
Alistair Bayley
-
Bayley, Alistair
-
Tomasz Zielonka