在haskell中构造 [英] construct in haskell

查看:127
本文介绍了在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处理

通过使如果进一步缩进< end < - hIsEof句柄,它实际上是一个续行,而不是。同样, putStrLn行少于行<-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屋!

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