22

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.

4 Answers 4

38

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"]
Sign up to request clarification or add additional context in comments.

4 Comments

This seems to be exactly what i need. Thanks!
I have done this. Now, how do I change the Seq a to a ?
@MickaelBergeronNéron you can get [a] out of Seq a with Data.Foldable.foldr (:) []. If you have an f :: a -> a -> a operation, you can call Data.Foldable.foldr f with some initial element (like 0), to get the combined a value.
@MickaelBergeronNéron we also get [a] out of Seq a with Data.Foldable.foldMap (:[]) (or with other Monoids, like e.g. Data.Foldable.foldMap Sum for numbers, etc.).
15

As far as I know (and can find) it does not exist by default. However, there exists splitAt in Data.List so:

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.

2 Comments

It's not O(n) in general but O(i), where i is the split index (because only the prefix needs to be copied). If that index is constant, the operation is O(1).
Always a good idea to include a type signature: replaceAtIndex :: Int -> a -> [a] -> [a]
10

There is actual arrays, but lists are really singly linked lists and the notion of replacing an element is not quite as obvious (and accessing an element at a given index may indicate that you shouldn't be using a list, so operations that might encourage it are avoided).

Comments

0

Try this solution:

import Data.List

replaceAtIndex :: Int -> a -> [a] -> [a]    
replaceAtIndex i x xs = take i xs ++ [x] ++ drop (i+1) xs

It works as follows:

get the first i items, add the value 'x', add the rest of i+1 items

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.