Arrays and arrays

Ketil Z. Malde ketil@ii.uib.no
21 Jan 2003 11:12:17 +0100


ketil@ii.uib.no (Ketil Z. Malde) writes:

> ketil@ii.uib.no (Ketil Z. Malde) writes:

>> Hal Daume III <hdaume@ISI.EDU> writes:
 
>>> Yes, (//) is terrible.  It *never* tries to update in-place.  

>   replace :: UArray Int Int -> [(Int,Int)] -> UArray Int Int
>   replace a p = runST (thaw a >>= \u -> update u p >> freeze u)
> 
>   update :: STUArray () Int Int -> [(Int,Int)] -> ST () ()
>   update u ps = mapM_ (uncurry (writeArray u)) ps

> I've tried rearranging the code in various ways, but I can get no
> further. 

It's funny, you know, how asking questions on the internet gets you
the answer quickly.  Just after sending this, I scratched my head, and
did:

        update :: STUArray s Int Int -> [(Int,Int)] -> ST s ()

and, well, that apparently worked.  Like a charm.  The 's' parameter
is apparently just magic, or in Marcin Kowalczyk's words (old (26 Feb
2001) post to the haskell list):

| The type variable 's' is used in a very tricky way, to ensure
| safety when
|    runST :: (forall s. ST s a) -> a
| is used to wrap the ST-monadic computation in a purely functional
| interface. It does not correspond to the type of data being
| manipulated.

(I'll be right back with the benchmarks.)

-kzm
-- 
If I haven't seen further, it is by standing in the footprints of giants