Lazy evaluation with a predicate

Viewed 247

I'm trying to write a function in Haskell that counts the elements in a list satisfying a predicate and returns True if the number exceeds some threshold. I have an implementation that looks like this:

hitsThreshold :: Int -> (a -> Bool) -> [a] -> Bool
hitsThreshold threshold test strs =
  (length $ filter test strs) >= threshold

The problem is, I want this to evaluate lazily so that it will terminate as soon as the length reaches the threshold. For example, I should be able to pass in an infinite list and it should terminate in finite time (assuming the threshold is reached eventually). Is there a simple way to do this?

1 Answers
Related