0

This function is defined in the book Real World Haskell.

--file ch03/Lending.hs
lend amount balance = let reserve = 100 
                      newBalance = balance - amount
                      in if balance < reserve
                         then Nothing
                         else Just newBalance

I try to run this in the interpreter, and end up with this error:

Lending.hs:3:54: parse error on input `='

Line 3 is "newBalance = balance - amount" I don't think this is a white space issue so I am genuinely confused.

Edited to align the two local variable declarations:

--file ch03/Lending.hs
lend amount balance = let reserve = 100
                          newBalance = balance - amount
                      in if balance < reserve
                         then Nothing
                         else Just newBalance

The error persists:

Lending.hs:3:68: parse error on input `='

duplode
  • 33,731
  • 7
  • 79
  • 150

1 Answers1

3

Haskell has some funky rules for whitespace and alignment. In short, when using let you need to ensure your symbols line-up with the same level of indentation:

lend amount balance = let reserve = 100 
                          newBalance = balance - amount
                      in if balance < reserve
                         then Nothing
                         else Just newBalance

It looks like you're mixing tabs and spaces to do alignment in Haskell. It's important to remember that Haskell is nothing like "curly-brace" languages (C, Java, etc) where whitespace is insignificant, in Haskell it matters, and getting the col/char difference between tabs and spaces right is painful (and varies from editor-to-editor) so it's best to stick to using only spaces for indentation and alignment in Haskell.

When I paste this into CompileOnline ( http://www.compileonline.com/compile_haskell_online.php ) with the extra line main = print (lend 5 500) it compiles fine.

Dai
  • 141,631
  • 28
  • 261
  • 374