Haskell - 是什么让'main'独一无二? [英] Haskell - What makes 'main' unique?

查看:186
本文介绍了Haskell - 是什么让'main'独一无二?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此代码:

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 value main. The value of the program is the value of the identifier main in module Main, which must be a computation of type IO t for some type t (see Chapter 7). When the program is executed, the computation main is performed, and its result (of type t) is discarded.

这篇关于Haskell - 是什么让'main'独一无二?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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