[Haskell-cafe] Mutable arrays

Stefan O'Rear stefanor at cox.net
Tue Feb 5 20:26:43 EST 2008

On Tue, Feb 05, 2008 at 06:00:38PM -0600, John Lato wrote:
> -- let ary_max = foldl1' max $ elems $ unsafeFreeze myArray
> If you use a boxed array type (IOArray or STArray) for myArray, and
> compiled with GHC, no copying is necessary (you may need to use type
> annotations to guarantee this).  Then use the foldl' function to get
> array_max, and map it onto the original mutable array.  I think it
> would be safe provided that you calculate ary_max before you start to
> modify the array, which is true for normalization.

Eek!  unsafeFreeze isn't a type cast, it actually modifies flags in the
heap object which are used by the generational garbage collector.  It's
quite concievable that you could get segfaults by modifying a boxed
array after passing it to unsafeFreeze.

This, I believe, would work:

let ary_max = foldl1' max [ inlinePerformIO (readArray myArray ix)
                            | ix <- range (inlinePerformIO (getBounds myArray)) ]

But it's equally untested.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20080205/6a07d40d/attachment.bin

More information about the Haskell-Cafe mailing list