Saturday, January 10, 2015

Circular Function calls in haskell.

-- Circular Function calls in haskell.
-- To understand how this works, imagine running `prime 11` in your mind
-- The key is lazy evaluation
prime :: Integer -> Bool
prime
| n < 1 = error "not a positive integer"
| n == 1 = False
| otherwise = ldp n == n where
-- ldp: least divising prime
ldp = ldpf primes
-- ldpf least divising prime from
ldpf (p:ps) m
| rem m p == 0 = p
| p^2 > m = m
| otherwise = ldpf ps m
primes = 2 : filter prime [3..]

0 comments: