在Haskell中写或不写`module Main where` [英] To write or not to write `module Main where` in Haskell

查看:334
本文介绍了在Haskell中写或不写`module Main where`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Haskell 98规范说,按照惯例,程序的入口点,即函数 main 应该驻留在名为Main的模块中。但是,即使你不写 main in的文件顶部的 module main其中,源代码编译并且在使用GHC时看起来正确。



问题是:


  1. 编写模块Main 并且不写它的区别是什么?

  2. 哪一个是首选?
  3. >


解决方案

没有真正的区别, module Main(main)where 在你没有自己指定头时是隐式定义。从 Haskell 98报告


模块的缩写形式(仅包含
模块体)是允许的。如果使用这种方法,那么头部被假定为
module Main(main),其中


我宁愿将一个显式定义转换为一个隐式定义,但对于一个 Main.hs 而言,这是一个次要偏好。

Haskell 98 specification says that the entry point of a program, namely, function main, should reside in the module called Main, by convention. However, even if you don't write module Main where at the top of the file you write main in, the source code compiles and seems working correct when you're using GHC.

The question is:

  1. What's the difference between writing module Main where and not writing it?
  2. Which one is preferred?

解决方案

There isn't really a difference, module Main (main) where would be the implicit definition when you don't specify a header yourself. From the Haskell 98 Report:

An abbreviated form of module, consisting only of the module body, is permitted. If this is used, the header is assumed to be module Main(main) where.

I would prefer an explicit definition to an implicit one but, for a Main.hs it's a minor preference.

这篇关于在Haskell中写或不写`module Main where`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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