From http://www.haskell.org/haskellwiki/Testing_primality, there is this code:
isPrime n = n > 1 &&
foldr (\p r -> p*p > n || ((n `rem` p) /= 0 && r)) True primes
Where primes is a list of prime numbers (possibly infinite).
Two questions:
- How would one read the lambda passed to
foldr
function - Since
foldr
starts from the right, why does this function work when it is passed an infinite list of primes? I'm guessing there is a short circuit built into the lambda?