I need to be able to apply a function to the nth element of a list. For example:
> doSomething (+5) 2 [1,2,3,4,5]
should return [1,7,3,4,5]
I have a function that can do this:
doSomething :: (a -> a) -> Int -> [a] -> [a]
doSomething f n xs = ys ++ [f x] ++ zs
where (ys, x:zs) = splitAt (n - 1) xs
but I'm new to Haskell, and so I'm sure (as with many simple functions in Haskell) there is a much better way of doing this.
doSomething
is certainly correct, and I could understand it very quickly, so I see nothing 'wrong' with it except for performance considerations (++ is potentially slow). – Alexander Vieth