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?
Sure it is:
number_of_elements = (length .) . filter
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With