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.

Nikita Pestrov
  • 5,876
  • 4
  • 31
  • 66
Stefan Bucur
  • 221
  • 1
  • 2
  • 3

4 Answers4

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"]
Chris Stryczynski
  • 30,145
  • 48
  • 175
  • 286
hammar
  • 138,522
  • 17
  • 304
  • 385
  • This seems to be exactly what i need. Thanks! – Stefan Bucur Apr 13 '12 at 10:01
  • I have done this. Now, how do I change the Seq a to a ? – Mickael Bergeron Néron Jun 18 '14 at 10:42
  • @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. – Will Ness Aug 09 '15 at 12:35
  • @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.). – Will Ness Aug 09 '15 at 13:33
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.

marinus
  • 926
  • 7
  • 16
  • 7
    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)`. – Niklas B. Apr 12 '12 at 23:53
  • 6
    Always a good idea to include a type signature: `replaceAtIndex :: Int -> a -> [a] -> [a]` – John J. Camilleri Sep 29 '14 at 12:17
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).

thor
  • 21,418
  • 31
  • 87
  • 173
geekosaur
  • 59,309
  • 11
  • 123
  • 114
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

Elletlar
  • 3,136
  • 7
  • 32
  • 38
cealex
  • 173
  • 7