Is there any built-in function to replace an element at a given index in haskell?
Example:
replaceAtIndex(2,"foo",["bar","bar","bar"])
Should give:
["bar", "bar", "foo"]
I know i could make my own function, but it just seems it should be built-in.
If you need to update elements at a specific index, lists aren't the most efficient data structure for that. You might want to consider using Seq
from Data.Sequence
instead, in which case the function you're looking for is update :: Int -> a -> Seq a -> Seq a
.
> import Data.Sequence > update 2 "foo" $ fromList ["bar", "bar", "bar"] fromList ["bar","bar","foo"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With