1

trying to work out why this won't compile? I just posted a similar issue about haskell's 'where' syntax.

primeFactors :: Int -> [Int]
primeFactors x = genPrimes x []
  where
    genPrimes x xs
      |x == 0 = []
      |isPrime x = x : genPrimes (x - 1) xs
      |otherwise = genPrimes (x - 1) xs

I'm getting a parse error on input '|'

The 'isPrime' function is defined here, and holds a similar structure and functions fine, what's the syntactic issue with 'primeFactors'?

isPrime :: Int -> Bool
isPrime a = go a (a - 1)
  where 
     go a b 
      |a == 1 || b == 1 = True
      |a `mod` b == 0 = False
      |otherwise = go a (b - 1)

Thanks.

3
  • My best guess is that you have an indentation mismatch. Check that you're not using a mixture of tabs and spaces. Commented Nov 7, 2014 at 16:39
  • I cannot see the issue right now - have you tabs in there? - BTW: I don't think your primeFactors works right - primeFactors 5 would return [5,3,2], you never change the xs inside to anything different then [], ... Commented Nov 7, 2014 at 16:39
  • I'd like to remind everyone that you can find out whether there are tabs by opening an edit window on the OP. Although in this case that doesn't identify exactly where the problem is - the indentation must have got more messed up on posting. The fact that SO's code blocks are indicated by starting each line with either a tab or 4 spaces doesn't help there. Commented Nov 8, 2014 at 2:12

1 Answer 1

2

On my platform from GHCi v.7.6.3 the compilation of those two succeeds. It is not a correct implementation but it is correct syntax. (What you've written is basically filter isPrime [x,x-1,..0] and you need to filter for divisibility by x too. You may also want repeated prime factors, which you could get by successive divisions; see the divMod operator which computes both div and mod together.)

It is possible that your text editor mixes up tabs and spaces. One of my favorite customizations of the text editor Kate is to show explicit tabs and trailing spaces; it helps a lot with that. If you have tabs, a tab in Haskell is defined to align with the next column divisible by 8 -- which is not your convention (you use 2, 4, and 5 spaces). If it appeared in front of go for example but not in front of your guards, you would have a de-dent in Haskell semantics rather than an indent.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.