user1920703
user1920703

Reputation:

What is wrong with this Haskell let-in indentation?

In the below Haskell code, I get an error "parse error on input 'in'". From what I've read, the indentation I've used should be fine. I in fact use 'let' and 'in' with similar indentation ('let' aligned with 'in') successfully elsewhere in my code. What rule have I broken that causes this 'in' to be a problem? If I add a space in front of 'in' (making it 1-space furthur in than 'let') it compiles fine.

runTests :: IO ()
runTests = do
             let results = fmap testLispExpr testLispText
                 resultStrings = fmap show results
                 putSeq (x:xs) = do putStrLn x
                                    putSeq xs
                 putSeq [] = return ()
             in putSeq resultStrings

All help is appreciated. Thanks in advance.

Upvotes: 4

Views: 333

Answers (1)

dfeuer
dfeuer

Reputation: 48611

let syntax in do blocks is a little funny. If you want to use in, you have to indent it to the right of the let. Alternatively, you can omit the in altogether, and the let bindings will be visible for the rest of the do block:

           do
             let ....
                 ....
             putSeq resultStrings
         --  ..... (more `do` statements)
         --  ^^ must all start on the same column

Edit

As Ørjan Johansen notes, the do isn't actually necessary at all in this case, since there's only one statement after let. So the other option is to omit the do while keeping the in keyword. And they also note that you can also write it as

runTests = mapM (print . testListExpr) testLispText

and really make things clean.

Upvotes: 7

Related Questions