Haskell does have efficient mutable arrays.
There is
STUArray
, which has the quite sophisticated but often just unnecessaryIx
-indexing methodology with many bounds-checks and little special optimisation, which makes it a bit slower than theoretically possible.All
Data.Vector
have very little overhead, make heavy use of stream fusion optimisation, prefer a simple, "list-like" interface. That means you can actually transfer your example very easily directly to immutable vectors, and may still get better performance than you might expect:import Data.Vector.Unboxed as VU type Vect = VU.Vector Int f :: Vect -> [[Int]] -> Vect f x indsList = VU.foldl g x indsList g :: Vect -> [Int] -> Vect g x inds = VU.zipWith h x [0..] -- h is just an example of modifications on the values. where h x i | i`elem`inds = x VU.! i + 1 | otherwise = x VU.! i
Yes, you probably want to work in the ST
monad for mutable updates. Not sure what you mean by "does such controls affect performance": there isn't really any "control" that's not also present in imperative languages, once the compiler has optimised away the proven type-safety. Which GHC can do quite well; you can get pretty close to C performance with Data.Vector.Unboxed
. There is always some inevitable overhead, but that has mostly to do with garbage-collection etc. issues, which you would also get in Java.
Since ST
and IO
are monads, the compiler can in fact do some more high-level optimisations that wouldn't be possible in an imperative language, though compiler's aren't really that far yet.
Performance, particularly of array operations, is discussed in many places, for instance in RWH.