The problem is that GHCi can't know that your list of primes
is ordered. Maybe after 1000000000000000 elements of primes
, there comes some element that is smaller than 10
, so it must be still included in the ouput? That's why ghci only prints the first elements of the result ([2,3,4,7
), but not the whole result. If you only want those, you can use takeWhile
, which stops after it encounters a number that doesn't match the predicate:
> takeWhile (<= 10) primes
[2,3,5,7] -- takeWhile stops after it encounters 7, because the predicate becomes false
> filter (<= 10) primes
[2,3,5,7 -- filter cannot stop, because there might still be a number later in the list that satisfies the predicate. This is what the list comprehension does.