remove nth element from list haskell
Removing the nth element from a list in Haskell
To remove the nth element from a list in Haskell, you can use the delete
function from the Data.List
module. Here's an example:
import Data.List (delete)
removeNth :: Int -> [a] -> [a]
removeNth n xs = delete (xs !! (n-1)) xs
In this example, the removeNth
function takes an index n
and a list xs
as input. It uses the !!
operator to access the nth element of the list (subtracting 1 from the index since lists are zero-indexed), and then uses the delete
function to remove that element from the list.
Note that this implementation assumes that the index n
is within the bounds of the list. If the index is out of bounds, an error will occur. If you need to handle out-of-bounds indices, you can add appropriate error handling to the function.
I hope this helps! Let me know if you have any further questions.