Search code examples
haskellwhere-clauseparse-error

Haskell - defining a function with guards inside a 'where'


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.


Solution

  • 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
    

    You need to start a new statement with your helper function for the guards to be sufficiently indented by comparison. (And you also forgot an argument, n.) This would also work:

    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
    

    but this doesn't:

    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
    

    The key point is that the | has to be further to the right than the function name.

    In general, starting a new line continues the previous one as long as it is further to the right. The guards have to continue on from the function name.