How does this nth Prime program terminate?

Of course, there’s more to the story, because if I asked for head (tail (sieve [2..])), I would have to start evaluating someMoreStuff, where someMoreStuff = sieve [x | x <- [3..], x mod 2 /= 0].

But crucially I wouldn’t have to evaluate all of it. In particular, I’d now start evaluating sieve [x | x <- [3..], x mod 2 /= 0], again using the definition of sieve.

To do this, I would need to determine whether [x | x <- xs, x mod p /= 0] is of the form [] or the form p : xs, to determine which pattern to match. So I have to start evaluating [x | x <- xs, x mod 2 /= 0]. Doing so reveals that the first element is 3, so I can now use the equation sieve (p:xs) = p : sieve [x | x <- xs, x mod 2 /= 0] to calculate that someMoreStuff = 3 : evenMoreStuff.

And so on.

This kind of this is indeed quite surprising when first seen, so your confusion is very understandable :slight_smile: