Haskell - 是什么让“主要"与众不同? [英] Haskell - What makes 'main' unique?

查看:24
本文介绍了Haskell - 是什么让“主要"与众不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此代码:

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 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 - 是什么让“主要"与众不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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