We can rewrite this in strict languages with lazy constructs. For example, in Scala (of course stream is not only lazily evaluated thing there)main = print primes primes = 2:filter is_prime [3,5..] is_prime n = all (\p-> n `mod` p /= 0) (takeWhile (\p-> p*p<=n) primes)
def main(args: Array[String]): Unit = {
val n = Integer.parseInt(args(0))
System.out.println(primes(ints(2)) take n toList)
}
def primes(nums: Stream[Int]): Stream[Int] =
Stream.cons(nums.head,
primes ((nums tail) filter (x => x % nums.head != 0)) )
def ints(n: Int): Stream[Int] =
Stream.cons(n, ints(n+1))
I think the Haskell solution is more compact due to syntactic sugar, curring and "parentheses-free-ness", *not* lazy evaluation.