一个涉及putStrLn的谜 [英] A mystery involving putStrLn

查看:67
本文介绍了一个涉及putStrLn的谜的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么下面的代码段会产生错误parse error on input ‘putStrLn’?

Why does the piece of code below produce the error parse error on input ‘putStrLn’?

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

<interactive>:11:4: error: parse error on input ‘putStrLn’

还有,为什么下面的代码会产生错误parse error on input ‘let’?

Also, why does the following piece of code produce the error parse error on input ‘let’?

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


 <interactive>:31:4: error: parse error on input ‘let’

推荐答案

两个代码段都存在相同的问题.如果将do块的第一个动作与do本身放在同一行,则仍然必须使do块中的其余动作缩进第一个动作.修复它的两种选择:

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!"

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

这篇关于一个涉及putStrLn的谜的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆