在haskell中构造 [英] construct in haskell
问题描述
我正在努力学习Haskell,并希望编写一个将文件内容打印到屏幕上的小程序。当我将它加载到GHCi中时,我得到以下错误:
lockquote
'do'结构中的最后一个语句必须是一个表达式
我知道这个问题已经在这里问过了: Haskell - do中的最后一个语句必须是表达式。
即使我的代码非常相似,但我仍然无法弄清楚问题所在。
模块Main(main)其中
如果有人可以向我指出问题,我会非常感激。
import System.IO
import System(getArgs)
$ b $ main :: IO()
main = do
args< - getArgs
inh < - openFile $ ReadMode head args
printFile inh
hClose inh
printFile :: Handle - > IO()
printFile handle = do
end< - hIsEOF handle
if end b $ b then return()
else do line< - hGetLine handle
putStrLn line
printFile handle
您的缩进被打破。这些更好:
printFile :: Handle - > IO()
printFile handle = do
end< - hIsEOF handle
if end b $ b then return()
else do line< - hGetLine handle
putStrLn行
printFile句柄
printFile :: Handle - > IO()
printFile handle = do
end< - hIsEOF handle
if end b $ b then return()
else do
line< - hGetLine处理
putStrLn行
printFile处理
通过使如果
进一步缩进<做
。同样,行<-hGetLine句柄
的事实意味着在
else
内)在那里结束。
I'm trying to learn Haskell and want to write a small program which prints the content of a file to the screen. When I load it into GHCi I get the following error:
The last statement in a 'do' construct must be an expression
I know this question has be asked already here: Haskell — "The last statement in a 'do' construct must be an expression".
Even though my code is very similar I still can't figure out the problem. If anyone could point out the problem to me I'd be very thankful.
module Main (main) where
import System.IO
import System(getArgs)
main :: IO()
main = do
args <- getArgs
inh <- openFile $ ReadMode head args
printFile inh
hClose inh
printFile :: Handle -> IO ()
printFile handle = do
end <- hIsEOF handle
if end
then return ()
else do line <- hGetLine handle
putStrLn line
printFile handle
Your indentation is broken. These are better:
printFile :: Handle -> IO ()
printFile handle = do
end <- hIsEOF handle
if end
then return ()
else do line <- hGetLine handle
putStrLn line
printFile handle
printFile :: Handle -> IO ()
printFile handle = do
end <- hIsEOF handle
if end
then return ()
else do
line <- hGetLine handle
putStrLn line
printFile handle
By having if
further indented than end <- hIsEof handle
, it was actually a line continuation, not a subsequent action in the do
. Similarly, the fact that you had putStrLn line
less indented than line <- hGetLine handle
means that the do
(inside the else
) ended there.
这篇关于在haskell中构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!