Haskell- List replacement function

Asked

Viewed 333 times

1

They know if there is a predefined function in Haskell that, given an element, replaces it with another element of a given list position. For example:

func 1 2 [1,2,3,4]  
[1,2,1,4]

1 answer

2

If you need to change elements in a given index, lists are not the structure for this. You could use Seq of Data.Sequence, in which case the function you are looking for is update :: Int -> a -> Seq a -> Seq a.

To do what you wanted in your example using the update would look like this:

> import Data.Sequence
> update 2 1 $ fromList [1,2,3,4]
fromList [1,2,1,4]

For more information see this link: https://stackoverflow.com/questions/10133361/haskell-replace-element-in-list

Browser other questions tagged

You are not signed in. Login or sign up in order to post.