[Haskell-cafe] REALLY simple STRef examples
Bulat Ziganshin
bulat.ziganshin at gmail.com
Thu Jul 20 15:02:47 EDT 2006
Hello Chad,
Thursday, July 20, 2006, 9:38:43 PM, you wrote:
> I suppose the same holds for runSTUArray, right? But this still gives
> me that same error, about being less polymorphic than expected.
there is well-known problem with that _unboxed_ arrays aren't
polymorphic. Oleg Kiselyov proposed solution to that problem that i implemented
in ArrayRef library (read http://www.haskell.org/pipermail/haskell-cafe/2004-July/006400.html
and http://haskell.org/haskellwiki/Library/ArrayRef)
this code compiles (i've also fixed pair of other problems):
import Data.ArrayBZ.IArray
import Data.ArrayBZ.ST
(+=) x y = let updateX i = do xi <- readArray x i
writeArray x i (xi + y!i)
in sequence_ . map updateX $ indices x
sumArrays [] = error "Can't apply sumArrays to an empty list"
sumArrays (x:xs) = runSTUArray (result x)
where
result x = do x0 <- thaw x
mapM_ (x0 +=) xs
return x0
ps: you successfully going through all the standard Haskell troubles in
this area :) seems that making FAQ about using ST monad will be a
good idea :)
--
Best regards,
Bulat mailto:Bulat.Ziganshin at gmail.com
More information about the Haskell-Cafe
mailing list