Delete all instances from list
functional-programming, haskell, recursion
Solution
First, the type signature is malformed.
deleteAllInstances :: (a, [l]) => a -> [l] -> [l]
A type signature has the form
name :: (Constraints) => type
where `Constraints` involve type classes, like `(Ord a, Show a)`. In this case, the function uses `(==)`, so there must be a constraint of the form `Eq a`.
Then the function definition doesn't match the type part, you defined it to take a pair as argument, while the type signature says otherwise (your definition is uncurried, the type is curried).
deleteAllInstances (a, []) = []
deleteAllInstances (i, (x:xs))
| i == x = tail
| otherwise = x ++ tail
where tail = deleteAllInstances i xs
then you use `(++)` to glue an element to the front of a list, but `(++)` concatenates two lists, you need `(:)` here.
The simplest way to define the function would be to use `filter`
deleteAllInstances :: Eq a => a -> [a] -> [a]
deleteAllInstances a xs = filter (/= a) xs
but if you want to do the explicit recursion yourself,
deleteAllInstances :: Eq a => a -> [a] -> [a]
deleteAllInstances a (x:xs)
| a == x = rest
| otherwise = x : rest
where
rest = deleteAllInstances a xs
deleteAllInstances _ _ = []
Problem
I'm trying to delete all instances of an item in a list using haskell. I get an error that I don't really understand. Can anyone help me out and let me know if I'm doing the correct thing? ``` deleteAllInstances :: (a, [l]) => a -> [l] -> [l] deleteAllInstances (a, []) = [] deleteAllInstances (i, (x:xs)) | i == x = tail | otherwise = x ++ tail where tail = deleteAllInstances i xs ```