Haskell: Invert filter predicate

Problem: In Haskell, you intend to filter with an inverted filter operator.

For example, your code is (GHCi):

Prelude> import Data.List
Prelude Data.List> filter ( isPrefixOf "a" ) ["a","ab","cd","abcd","xyz"]
["a","ab","abcd"]

The list you need is ["cd","yz"]. In some cases, the easiest solution would be to use for example <= instead of >, but there isn’t a literal complement for all functions, like for example isPrefixOf, which is being used in the example.

Solution:

You can simply compose the not function (composition is done by using the composition operator .) to negate the result of the predicate.

Prelude> import Data.List
Prelude Data.List> filter ( not . isPrefixOf "a" ) ["a","ab","cd","abcd","xyz"]
["cd","xyz"]

Note that not isPrefixOf "a" (note the missing composition operator) won’t work because you’re applying not to a curried function. notcan only by applied to a Bool.

A detailed, more general (but far more complex) solution is described in this StackOverflow post