In the book of The Haskell Road to Logic, Math and Programming by Doets et.al., at page 103, it is given that
prime :: Integer -> Bool
prime n | n < 1 = error "not a positive integer"
| n == 1 = False
| otherwise = ldp n == n where
ldp = ldpf primes
ldpf (p:ps) m | rem m p == 0 = p
| p^2 > m = m
| otherwise = ldpf ps m
primes = 2 : filter prime [3..]
However, isn't the logic of this function circular ? I mean I think I'm quite comfortable with recursive functions, but I do not think that this is a recursive function. Even if it is, I'm not able to comprehend the logic of it, so my question is that how to read the logic of such a function.
Edit:
The reason why I'm confused is prime
function uses the list primes
, but to generate that list, we also use the function prime
, so it seems that there is a circular logic, or I just do not understand recursive functions with lazy evaluation.