Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 963
Lukasz Sliwa Avatar asked Dec 23 '11 22:12

Lukasz Sliwa


1 Answers

Sure it is:

number_of_elements = (length .) . filter
like image 163
Rotsor Avatar answered Oct 06 '22 00:10

Rotsor