[Haskell-cafe] Re: Go Haskell! -> array libraries
Dan Doel
dan.doel at gmail.com
Sun Nov 30 12:44:16 EST 2008
On Sunday 30 November 2008 6:28:29 am Roman Leshchinskiy wrote:
> On 30/11/2008, at 11:36, Don Stewart wrote:
> > Should mutable arrays have list-like APIs? All the usual operations,
> > just in-place and destructive where appropriate?
>
> I don't know. To be honest, I don't think that the term "mutable
> array" describes a single data structure. For instance, one of the
> central questions which unveils a whole bunch of design possibilities
> is: can mutable arrays be concatenated and how does that work if yes?
I don't know about concatenation, but it is useful for them to be able to be
zipped. For instance:
schwartz f arr = do keys <- cloneWith f arr
pairs <- zip keys arr
sortBy (comparing fst) pairs
(cloneWith should construct a new array where new[i] = f (old[i])) is a
mutable sort of arr using the comparison
compare e e' = compare (f e) (f e')
caching the results of f for each element. This, obviously, takes advantage
of the fact that sorting the zipped array mutably updates the underlying
(original) arrays.
On the other hand, I could see such an operation easily leading to bugs if it
slips your mind that changing one array can effect another (maybe it should
be called unsafeZip :)).
-- Dan
More information about the Haskell-Cafe
mailing list