Haskell - 是什么让“主要"与众不同? [英] Haskell - What makes 'main' unique?
问题描述
使用此代码:
main :: FilePath -> FilePath -> IO ()
main wrPath rdPath = do x <- readFile rdPath
writeFile wrPath x
我收到以下错误:
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
?
推荐答案
因为语言规范 这样说.
Haskell 程序是一组模块,按照惯例,其中一个模块必须称为 Main
,并且必须导出值 main
.程序的值是模块Main
中标识符main
的值,对于某种类型IO t
,它必须是类型IO t
的计算code>t(见第 7 章).程序执行时,执行计算main
,其结果(t
类型)被丢弃.
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 - 是什么让“主要"与众不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!