[Haskell-cafe] Advice needed on best way to simulate an STL vector

Brian Hulley brianh at metamilk.com
Wed Apr 19 12:07:50 EDT 2006


Hi -
In C++, STL provides a vector class which behaves as an array except you can 
insert/delete elements from it. I'm wondering what is the best Haskell data 
structure to use to simulate this, either mutable or immutable.

I've looked at the Array interface, but although there is the // operation 
to replace some elements, there does not appear to be a simple way to 
delete/insert elements.

Ideally I'd like functions like:

-- Insert new elements starting at the specified index, moving the others up 
to make room
insert:: Array i e -> i -> [e] -> Array i e

-- Delete a range of elements, moving later elements back down
delete:: Array i e -> i -> i -> Array e

-- Append a new element to the end of an array
push_back :: Array i e -> e -> Array i e

Is there an efficient way of implementing these operations in Haskell, based 
on arrays, or should I be using some other data structure altogether eg a 
list?

Also, for large arrays, am I right in thinking that it is still more 
efficient to use immutable arrays rather than mutable arrays (because it is 
easier for gc to always just deal with immutable values)?

Thanks, Brian. 



More information about the Haskell-Cafe mailing list