为什么 Int maxBound 不起作用?
当我在 ghci 中尝试时
> Int maxBound
,我得到
Not in scope: data constructor 'Int'
Even if I import Data.Int
, still the Problem persists.这是怎么回事?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编辑:该函数的官方文档位于 http://www.haskell.org/ghc/docs/7.0.3/html/libraries/base-4.3.1.0/Prelude.html#v:maxBound
首先, 您应该这样做
如果您查看
maxBound
的类型签名,:那么
maxBound
是一个返回a
类型的函数,其中a
是有界
。但是,它不接受任何参数。Int maxBound
表示您正在尝试使用数据构造函数Int
和参数maxBound
创建某些内容。对于您的特定错误消息,您尝试使用
Int
- 这是一种类型 - 作为值,从而导致您收到错误。导入 Data.Int 没有帮助。EDIT: The official docs for the function are at http://www.haskell.org/ghc/docs/7.0.3/html/libraries/base-4.3.1.0/Prelude.html#v:maxBound
To begin with, you should be doing
If you look at the type signature of
maxBound
:Then
maxBound
is a function that returns something of typea
, wherea
isBounded
. However, it does not accept any parameters.Int maxBound
means that you are trying to create something with data constructorInt
and parametermaxBound
.For your specific error message, you are trying to use
Int
- which is a type - as a value, resulting in the error you are getting. ImportingData.Int
won't help.那不是有效的 Haskell。
maxBound
< /a> 是一个常量,定义Bounded
类< /a>:要获取任何特定类型的绑定,您需要将其专门化为特定类型。 类型注释< /a> 由表达式的
::
语法给出,如下所示:That's not valid Haskell.
maxBound
is a constant that defines the maximum element of types that are in theBounded
class:To get the bound for any particular type, you need to specialize it to a particular type. Type annotations are given by
::
syntax on expressions, like so:即使这种情况 (maxBound) 不是 TypeApplications GHC 编译器选项最有效的用法,我发现它与类型注释选项 :: (至少对于我这个刚踏上 Haskell 船的人来说)。
我承认它需要编译选项/编译指示,但它是我在 ~/.ghci 文件中全局激活的众多选项之一。而且申请起来很容易!
因此,事不宜迟,让我们在实践中看看它。以下是对我来说启用 TypeApplications 编译器选项的最常见选项。
然后在 GHCI 提示符下,
注释:
Even if this case (maxBound) is not the most elocvent usage of the TypeApplications GHC compiler option, I found it a bit more suggestive compared with the type annotation options :: (at least for me as a newly embarked on Haskell boat).
I admit it requires a compilation option/pragma, but it is one of many I have activated by globally in ~/.ghci file. And it's easy to apply!
So, without further ado let's see it in practice. The followings are for me the most common options to enable the TypeApplications compiler option.
and then in GHCI prompt
Notes: