Reputation: 718
Hmm... why does this function ends in an infinite loop when evaluating for any integer > 3?
smallestMultiple n = factors [2..n] where
factors [] = []
factors (h:xs) = h:(factors $ filter ((<)1) [div x h|x<-xs])
where
div x y
|x `mod` y ==0 = x `div` y
|otherwise = x
Upvotes: 0
Views: 150
Reputation: 48580
It appears that the main problem is that you define a local version of div
:
div x y
| x `mod` y == 0 = x `div` y
| otherwise = x
Since bindings in where
clauses (as well as in let
) are recursive, the div
on the right hand side of the first case refers to the same div
you're defining! You can fix this by using a different name, like div'
:
div' x y
| x `mod` y == 0 = x `div` y
| otherwise = x
Upvotes: 5