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

16,796
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.

Share:
16,796
Benjamin Hodgson
Author by

Benjamin Hodgson

I work here! It takes a lot of self-restraint for me not to abuse my live database privileges to artificially inflate my reputation score. [email protected]

Updated on June 05, 2022

Comments

  • Benjamin Hodgson
    Benjamin Hodgson almost 2 years

    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.