Remove all instances from the list.

I am trying to remove all instances of an item in a list using haskell. I am getting an error that I really don't understand. Can someone help me and let me know if I am doing the right thing?

deleteAllInstances :: (a, [l]) =>  a -> [l] -> [l]
deleteAllInstances (a, []) = []
deleteAllInstances (i, (x:xs))
    | i == x = tail
    | otherwise = x ++ tail
    where tail = deleteAllInstances i xs
+3
source share
3 answers

First, the type signature is incorrect.

deleteAllInstances :: (a, [l]) =>  a -> [l] -> [l]

The type signature has the form

name :: (Constraints) => type

where Constraintsinclude type classes such as (Ord a, Show a). In this case, the function uses (==), so there should be a form limitation Eq a.

Then the definition of the function does not correspond to the part of the type, you defined it to take a pair as an argument, while the signature of the type says otherwise (your definition is not fulfilled, the type has the value curried).

deleteAllInstances (a, []) = []
deleteAllInstances (i, (x:xs))
    | i == x = tail
    | otherwise = x ++ tail
    where tail = deleteAllInstances i xs

(++) , (++) , (:) .

- filter

deleteAllInstances :: Eq a => a -> [a] -> [a]
deleteAllInstances a xs = filter (/= a) xs

,

deleteAllInstances :: Eq a => a -> [a] -> [a]
deleteAllInstances a (x:xs)
    | a == x    = rest
    | otherwise = x : rest
      where
        rest = deleteAllInstances a xs
deleteAllInstances _ _ = []
+9

, (a, [l]) =>, , . , a l .

, , a [l], . - . , , , .

deleteAllInstances :: a -> [l] -> [l]
deleteAllInstances a [] = []
deleteAllInstances i (x:xs)
    | i == x = rest
    | otherwise = x : rest
    where rest = deleteAllInstances i xs

filter,

deleteAllInstances :: a -> [a] -> [a]
deleteAllInstances a = filter (/=a)
+3

I really find that list comprehension is a very intuitive notation for such problems:

deleteAllInstances :: Eq a => a -> [a] -> [a]
deleteAllInstances a list = [x | x <- list, x /= a]
+3
source

All Articles