I'm just starting out at teaching myself Haskell. This code is supposed to do prime factorisation:
divides :: Integer -> Integer -> Bool
divides small big = (big `mod` small == 0)
lowestDivisor :: Integer -> Integer
lowestDivisor n = lowestDivisorHelper 2 n
where lowestDivisorHelper m n
| (m `divides` n) = m -- these should belong to lowestDivisorHelper
| otherwise = lowestDivisorHelper (m+1) n
primeFactors :: Integer -> [Integer]
primeFactors 1 = []
primeFactors n
| n < 1 = error "Must be positive"
| otherwise = let m = lowestDivisor n
in m:primeFactors (n/m)
I get a parse error on the commented line. I think my problem might be that lowestDivisorHelper
has guards, but the compiler doesn't know whether the guards belong to lowestDivisorHelper
or lowestDivisor
. How do I get around this?
I should add that I didn't want to define the helper function at the top level in order to hide the implementation detail. Importing the file shouldn't take the helper function with it.