[x | x <- [1..], x < 1000]
is equivalent to filter (< 1000) [1..]
; you want takeWhile (< 1000) [1..]
instead.
So what's the difference between filter
and takeWhile
?
Well, if you try to evaluate the entire result --- and that's what ghci does, in order to print it --- then filter
will test each and every element in the input list to determine whether it should be in the output list. After the first thousand elements? It carries on testing. filter
doesn't know that it isn't suddenly going to encounter ..., 12345, 12346, -7, 12348, ...
.
Another way of looking at it is that filter
can only say "the output list ends here" once it's reached the end of the input list. If you feed it an infinite list, it can never be sure it has generated all elements of the output list. So it will appear to hang.
takeWhile
, on the other hand, stops and terminates its output list as soon as it reaches an element that fails the condition.