Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell replace element in list

Tags:

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.

like image 523
Stefan Bucur Avatar asked Apr 12 '12 23:04

Stefan Bucur


1 Answers

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"] 
like image 70
hammar Avatar answered Sep 22 '22 14:09

hammar