Jestyn Strik
Jestyn Strik

Reputation: 11

How can I fix this function

I've been working on a school assignment using Haskell and keep getting the same parse-errors over and over again. I've been looking online and have gotten no solutions to my problem. Any advice would be greatly appreciated. Here is the function...

maxGaussNorm :: [GaussianInt] -> GaussianInt
maxGaussNorm [] = (0,0)
maxGaussNorm gs = maxAux (tail gs) (head gs)

maxAux :: [GaussianInt] -> GaussianInt -> GaussianInt
maxAux gs m 
      | gs == [] = m
      | (gaussNorm (head gs)) <= (gaussNorm m) = maxAux (tail gs) m
      | (gaussNorm (head gs)) > (gaussNorm m) = maxAux (tail gs) (head gs)

Try it online!

Upvotes: 0

Views: 102

Answers (1)

Potato44
Potato44

Reputation: 208

The code you have posted in the link is different from what you have posted in the question body.

Here is your code that actually has parse errors

maxGaussNorm :: [GaussianInt] -> GaussianInt
maxGaussNorm gs = compareMax (tail gs) (head gs)
    compareMax :: [GaussianInt] -> GaussianInt -> GaussianInt
    compareMax gs m
        gs == [] = m
        gaussNorm(head gs) <= gaussNorm m = compareMax (tail gs) m
        gaussNorm(head gs) > gaussNorm m = compareMax (tail gs) (head gs)

There are two things wrong with this, missing the pipe character | on the guards, and you forgot to include a where keyword before the definition of compareMax.

Here is the code with these parse error fixed

maxGaussNorm :: [GaussianInt] -> GaussianInt
maxGaussNorm gs = compareMax (tail gs) (head gs)
  where
    compareMax :: [GaussianInt] -> GaussianInt -> GaussianInt
    compareMax gs m
       | gs == [] = m
       | gaussNorm(head gs) <= gaussNorm m = compareMax (tail gs) m
       | gaussNorm(head gs) > gaussNorm m = compareMax (tail gs) (head gs)

The code you originally posted with the question also is a solution to the parse errors by rewriting the helper function as a top level function and including the pipes on the guards

maxGaussNorm :: [GaussianInt] -> GaussianInt
maxGaussNorm [] = (0,0)
maxGaussNorm gs = maxAux (tail gs) (head gs)

maxAux :: [GaussianInt] -> GaussianInt -> GaussianInt
maxAux gs m 
      | gs == [] = m
      | (gaussNorm (head gs)) <= (gaussNorm m) = maxAux (tail gs) m
      | (gaussNorm (head gs)) > (gaussNorm m) = maxAux (tail gs) (head gs)

This implementation is also slightly different in that it handles empty lists instead of erroring.

Upvotes: 2

Related Questions