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
First, the type signature is malformed.
A type signature has the form
where
Constraintsinvolve type classes, like(Ord a, Show a). In this case, the function uses(==), so there must be a constraint of the formEq 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).
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
filterbut if you want to do the explicit recursion yourself,