- 论坛徽章:
- 0
|
在real world haskell 里面看到一个例子. 就比葫芦画瓢的写了一遍.
结果从编译失败. 每个字母都检查了一下, 一模一样. 最后复制 例子 , 编译通过.
最后发现是缩进的问题. 代码如下:
正确的代码如下:
import System.IO
import Data.Char(toUpper)
main :: IO ()
main = do
inh <- openFile "input.txt" ReadMode
outh <- openFile "output.txt" WriteMode
mainloop inh outh
hClose inh
hClose outh
mainloop :: Handle -> Handle -> IO ()
mainloop inh outh =
do ineof <- hIsEOF inh
if ineof
then return ()
else do inpStr <- hGetLine inh
hPutStrLn outh (map toUpper inpStr)
mainloop inh outh
不正确的代码如下:
import System.IO
import Data.Char(toUpper)
main :: IO ()
main = do
inh <- openFile "input.txt" ReadMode
outh <- openFile "output.txt" WriteMode
mainloop inh outh
hClose inh
hClose outh
mainloop :: Handle -> Handle -> IO ()
mainloop inh outh =
do ineof <- hIsEOF inh
if ineof
then return ()
else do inpStr <- hGetLine inh
hPutStrLn outh (map toUpper inpStr)
mainloop inh outh
编译的时候提示:
[1 of 1] Compiling Main ( interactwith.hs, interactwith.o )
interactwith.hs:17:19:
The last statement in a 'do' construct must be an expression
谁能解释一下, haskell 的缩进是怎么回事? 应该怎么处理. 本来一个小程序, 结果折腾了一个上午. 非常感谢. |
|