2012-01-02 19 views
2

编译器说的Haskell - 语法做块(使用IO)

The last statement in a 'do' construct must be an expression: 
rmax <- getInteger 

试图加载包含下面的代码片断的文件时:

getInteger :: IO Integer 
getInteger = readLn 

main :: IO() 
main = do 
    putStrLn "specify upper limit of results" 
    rmax <- getInteger 
    if rmax `notElem` mot 
     then do putStrLn "run again and enter a multiple of 10" 
     else do print pAllSorted 

这是什么(编译器消息)的意思,为什么会发生在这里? (而它不会在:)

main = do 
    line <- getLine 
    if null line 
     then return() 
     else do 
      putStrLn $ reverseWords line 
      main 

reverseWords :: String -> String 
reverseWords = unwords . map reverse . words 

(例如,从http://learnyouahaskell.com/input-and-output拍摄)以上

+4

由于rmax < - rhing不是根据您发布的内容的do构造中的最后一行,因此您可能会遇到缩进问题。确保你在任何地方都没有制表符。 – Ingo 2012-01-02 11:20:39

回答

6

你缩进可能搞砸,因为混合制表符和空格组成。事实上,在你的问题的代码片段中似乎有一个流浪标签,我假设你直接从源文件粘贴。

很可能,GHC解释标签的方式与您的编辑器显示方式不同,所以它认为do块在该行后面结束。作为一个经验法则,最好在Haskell中只使用空格。该语言为大多数代码编辑人员不同意的解释标签定义了非常具体的规则,但空格是明确的和一致的。

+1

这是...谢谢。现在我已将记事本++设置更改为不使用制表符。 – 2012-01-02 11:26:58