The code works well
primes = next [2 ..]
where
next (p : ps) = p : next ts
where
ts = filter (\x -> mod x p /= 0) ps
Just GHCI think there is a incomplete patter in next.
Well, this is correct from a grammatical point of view.
But obviously the input of 'next' cannot be empty.
So is there a solution other than adding the declaration
({-# OPTIONS_GHC -Wno-incomplete-patterns #-})?
The exhaustiveness checker knows that
nexthas typeNum a => [a] -> [a]. The empty list is a valid argument tonext, even if you never actually callnexton the empty list.The key here is that you don't really want
Num a => [a]as your argument type. You know it will only be called on an infinite list, so use a type that doesn't have finite lists as values.The
Streamlibrary provides a moduleData.Streamthat defines theStreamtype and numerous analogs to list functions.