You can highlight the problem with the tracking code:
var counter1, counter2 = 0 def sieve1(s: Stream[Int]): Stream[Int] = { counter1 += 1 s.head
We can run this and check the counters:
scala> counter1 res2: Int = 100 scala> counter2 res3: Int = 540
Thus, in the first case, the depth of the call stack is the number of primes, and in the second, the largest prime (well, minus one).
source share