14

I'm trying to nest a couple let statements, but I'm getting syntax errors that don't make sense to me. I'm really new to Haskell programming so I'm sure it's something I just don't understand (probably having to do with the spacing). I understand that let and in must be in the same column.

Why is it that:

aaa = let y = 1+2
          z = 4+6
      in y+z

Works perfectly fine, whereas

aaa = let y = 1+2
          z = 4+6
          in let f = 3
                 e = 3
             in e+f

gives me the error: "Syntax error in expression (unexpected `=')"

Nico
  • 1,181
  • 2
  • 17
  • 35
  • 4
    Off-topic, but let-expressions are recursively bound by default, so nesting a let inside a let is only rarely necessary. – John L Apr 14 '12 at 07:56
  • What John L means is that you can combine the inner and outer let statements into a single statement. – Gabriella Gonzalez Apr 14 '12 at 15:12
  • 1
    Right, I understand that, this was for educational purposes. I was trying to figure out if the language was statically or dynamically scoped without referring to a manual. – Nico Apr 14 '12 at 16:37

1 Answers1

21

In the second example, the z = ... isn't aligned with the y = .... In a let block, every definition has to be aligned.

I suspect you're indenting with tab characters, and have your editor set to display tabs as less than 8 spaces, making it look like it's aligned to you. You should replace the tab with spaces, and preferably set your editor to expand tabs into spaces to avoid problems like this in the future.

ehird
  • 40,602
  • 3
  • 180
  • 182
  • That misalignment was actually my fault when I copied my code to my browser but you were right about using spaces instead of tabs. I had a few tabs in it and it completely messed everything up. Replaced it with spaces and it worked like a charm, thank you very much! – Nico Apr 13 '12 at 21:55
  • 3
    @Nico I would suggest you revert the edit, so it matches the explanation in this answer, that way people that find this won't get confused. – chamini2 Mar 01 '16 at 02:37