Is there any built-in function to replace an element at a given index in haskell?
Example:
replaceAtIndex(2,\"foo\",[\"bar\",\"bar\",\"bar\"])>
replaceAtIndex(2,\"foo\",[\"bar\",\"bar\",\"bar\"])
As far as I know (and can find) it does not exist by default. However, there exists splitAt in Data.List so:
splitAt
Data.List
replaceAtIndex n item ls = a ++ (item:b) where (a, (_:b)) = splitAt n ls
This is O(N) though. If you find yourself doing this a lot, look at another datatype such as array.