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.

Stefan

Attachment: signature.asc
Description: Digital signature

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to