Filtering an infinite list in Haskell

后端 未结 1 1002
星月不相逢
星月不相逢 2021-01-13 08:15

I have the following code that implements the Sieve of Eratosthenes:

primes :: [Int]
primes = primes\' [2..]

primes\' :: [Int] -> [Int]
primes\' [] = []
         


        
1条回答
  •  清酒与你
    2021-01-13 08:35

    In cases like this where you know that once the predicate returns false for an element, it won't ever return true for a later element, you can replace filter with takeWhile, which stops taking elements as soon as the predicate returns false for the first time.

    0 讨论(0)
提交回复
热议问题