Haskell - 是什么让'main'独一无二? [英] Haskell - What makes 'main' unique?
问题描述
使用此代码:
With this code:
main :: FilePath -> FilePath -> IO ()
main wrPath rdPath = do x <- readFile rdPath
writeFile wrPath x
我得到以下错误:
$ b
I got the following error:
Couldn't match expected type 'IO t0'
with actual type 'FilePath -> FilePath -> IO()
但是,当我将'main'的名称更改为其他名称时,该文件正确编译。
But the file compiles correctly when I change the name of 'main' to something else.
关于main的独特之处是什么,为什么它的类型必须是 IO t0
?
What's so unique about main and why does its type have to be IO t0
?
推荐答案
因为语言规范这样说。
$ b
Because the language spec says so.
Haskell程序是一个模块集合,按照惯例,其中一个必须被称为
Main
,并且必须导出值main
。程序的值是模块Main
中标识符main
的值,它必须是类型<对于某些类型t
来说,code> IO t (见第7章)。当程序执行时,计算main
被执行,其结果(类型t
)被丢弃。 / p>
A Haskell program is a collection of modules, one of which, by convention, must be called
Main
and must export the valuemain
. The value of the program is the value of the identifiermain
in moduleMain
, which must be a computation of typeIO t
for some typet
(see Chapter 7). When the program is executed, the computationmain
is performed, and its result (of typet
) is discarded.
这篇关于Haskell - 是什么让'main'独一无二?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!