2

I'm writing some Haskell code to learn the language, and I've run into the syntax error:

Vec2.hs:33:27: parse error on input '='

The code I've written here is below. The error is pointing at the 2nd term in vec2Normalize iLength = ... I don't see the syntax error

-- Get the inverse length of v and multiply the components by it
-- Resulting in the normalized form of v
vec2Normalize :: Vec2 -> Vec2
vec2Normalize v@(x,y) = (x * iLength, y * iLength)
    where length = vec2Length v
          iLength = if length == 0 then 1 else (1 / length)
Tom Tetlaw
  • 83
  • 1
  • 10
  • 21
  • That's not line 33 of what you pasted. It's quite possible the error is somewhere else, and only being reported on line 33. Whatever line 33 happens to be. – Carl Jun 01 '13 at 07:07
  • It's line 33 of the file that it's in, I only posted the relevant part. When I comment out this function the error goes away. – Tom Tetlaw Jun 01 '13 at 07:11

2 Answers2

7

Some guessing involved since you don’t provide the complete code, but this error could indicate that your line iLength = ... is not properly indented; actually, that the iLength starts to the right of the length = on the line before.

Does your original file use tabs instead of spaces for indentation? If so, be aware that Haskell always interprets a tab as spanning 8 columns. So, e.g.,

<TAB>where length = ...
<TAB><TAB><SPACE><SPACE>iLength = ...

would be interpreted as

        where length = ...
                  iLength = ...

thus causing the error, even though your editor might show the lines properly aligned if it uses 4-column tabs.

chirlu
  • 3,141
  • 2
  • 21
  • 22
  • 3
    Yes, the code is using tabs. You can tell by clicking the "edit" link on the question and trying to select the whitespace on that line. It will also look just like your second code block. – hammar Jun 01 '13 at 07:37
4

You are using tabs for indentation, so the second definition in the where clause is actually not aligned with the first one. Haskell uses a tab width of 8 spaces, which may be different from your editor, leading to problems like this where the code looks okay, but really isn't.

I strongly recommend that you configure your editor to use spaces only when working with Haskell code.

hammar
  • 138,522
  • 17
  • 304
  • 385