1
4

2 回答 2

13

Both snippets have the same problem. If you put the first action of a do block on the same line as the do itself, you still have to indent the rest of the actions in the do block as far as the first one. Two choices to fix it:

main = do line <- fmap reverse getLine
          putStrLn $ "You said " ++ line ++ " backwards!"
          putStrLn $ "Yes, you said " ++ line ++ " backwards!"

or

main = do
   line <- fmap reverse getLine
   putStrLn $ "You said " ++ line ++ " backwards!"
   putStrLn $ "Yes, you said " ++ line ++ " backwards!"
于 2019-02-20T21:39:58.540 回答
3

It also works when explicit separators are used throughout:

main = do { line <- fmap reverse getLine ;
   putStrLn $ "You said " ++ line ++ " backwards!" ;
   putStrLn $ "Yes, you said " ++ line ++ " backwards!" }

main = do { line <- getLine ;
   let { line' = reverse line } ;                     -- NB let's { }s
   putStrLn $ "You said " ++ line' ++ " backwards!" ;
   putStrLn $ "Yes, you said " ++ line' ++ " backwards!" }

This is not a substitute for the good indentation style, but an addition to it.

于 2019-02-20T23:55:13.510 回答