[Haskell-cafe] Mutable arrays

Jeff φ jeff1.61803 at gmail.com
Tue Feb 5 11:36:27 EST 2008

 I want to say thanks to everyone who responded to my mutable array post.
I'm going to work through and experiment with all the comments people
posted.  It might take me a while.

Luke Palmer wrote:

> Hmm, how big is the array?   If it's pretty big, that's
> understandable.  Frankly, it's because foldl sucks: I have never seen
> a reason to use it.  You should be using the strict variant foldl'
> here.  (I don't think there is a foldl1').  And that will get rid of
> your big function calc_max_2d_elem.

I should have mentioned that I'm working with a 2D array that is 1024 x
1024.  Eventually, this code will have to work with arrays that are much
larger.  (For fun I write image processing and fractal "art" programs.)  I
replaced the foldl1 with foldl1'.  Unfortunately, I still get a stack

Chaddaï Fouché wrote:

> Sorry but none of those propositions change the heart of the problem :
> the list of elements is totally produced before she can be consumed
> due to the strict monadic (IO or ST) nature of getElems. Thus you get
> an extraordinary waste of memory as well as resources...

This is interesting.  I've been programming in Concurrent Clean for a
while.  Instead of monads, Clean supports unique types for mutable arrays
and IO.  In Clean, I can write code that iterates through a mutable array by
converting it to a lazy list.  This is convenient because I can use all the
nice list processing functions that are available.

Changing the subject slightly, I once wrote code in Concurrent Clean that
filtered a file that was larger than the available memory on my PC.  I did
this by creating a function that returned the contents of the original file
as a lazy list.  Then, I created functions to process the list and write the
processed list to a results file.  The code was not imperative at all.  The
function that wrote the results file forced the evaluation of the lazy
list.  As the lazy list was consumed, the contents of the original file were
read.  Is this possible with Monads in Haskell?  Based on your comments, I
suspect that in Haskell, one would have to explicitly code a loop that reads
a portion of the original file, processed it, and writes a portion of the
results file, over and over.

By the way, if anyone wants to see it, I can post some Clean code that
demonstrates the file processing I described.  Clean's syntax is very
similar to Haskell's.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20080205/71c3c85c/attachment.htm

More information about the Haskell-Cafe mailing list