[Haskell-cafe] Prime numbers

Bill Wood william.wood3 at comcast.net
Tue Dec 20 15:36:10 EST 2005


On Tue, 2005-12-20 at 16:53 +0000, Daniel Carrera wrote:
> Henning Thielemann wrote:
> >>factors :: Integer -> Integer -> Bool
> >>factors m n | m == n = False
> >>             | m < n  = divides m n || factors (m+1) n
> > 
> > 
> > Btw. I find the recursion harder to understand than the explicit
> > definition:
> > 
> > factors n = any (divides n) [2..(n-1)]
> 
> For what it's worth, I also found this function un-intuitive. What I'd 
> expect a function called "factors" to do is exactly what yours does, and 
> not what the one in the example does.

It helped me to read "factors m n" as "there is an integer between m and
n-1 inclusive that divides n".  Then the recursion made sense.
Thielemann's "factors n" would read "there is an integer between 2 and
n-1 inclusive that divides n".

 -- Bill Wood




More information about the Haskell-Cafe mailing list