Search code examples
haskellfilterpointfree

Number of elements in Haskell in pointfree style


I want to define a function that computes the number of elements in a list that satisfy a given predicate:

  number_of_elements :: (a -> Bool) -> [a] -> Int
  number_of_elements f xs = length (filter f xs)

For example:

  number_of_elements (==2) [2,1,54,1,2]

should return 2.

We can write it shorter:

  number_of_elements f = length . filter f

Is it possible to write it without f parameter?


Solution

  • Sure it is:

    number_of_elements = (length .) . filter