问题描述
在我遇到的一些 Haskell 代码中:
In some Haskell code I came across:
put :: s -> m ()
这里的 ()
是什么意思?
What does the ()
mean here?
我会使用搜索引擎,但找不到正确处理 ()
的搜索引擎.
I'd use a search engine, but I can't find one that handles ()
correctly.
推荐答案
如果不是奇怪的特殊语法,可以定义为
If not for the weird special syntax, it could be defined as
data () = ()
这是最无聊的类型.语法应该让你想到元组:(a,b)
是一对,(a,b,c)
是一个三元组,等等,()
是一个 0 元组.唯一缺少的是一个 1 元组,它不能有这种语法,因为它会与通常使用的括号冲突.
It's about the most boring type there is. The syntax is supposed to make you think of tuples: (a,b)
is a pair, (a,b,c)
is a triple, etc., and ()
is a 0-tuple. The only thing missing is a 1-tuple, which can't have that syntax because it would clash with the usual use of parentheses.
()
经常被用作没有有趣结果的结果.例如,一个 IO
操作应该执行一些 I/O 并终止而不产生结果,它通常具有类型 IO ()
.当你需要一些无趣的输入时也可以使用它;GHC 对此有特殊的语法,但在 Haskell 98 中模仿 Lisp 的 cond
的方式是这样的:
()
is very often used as the result of something that has no interesting result. For example, an IO
action that is supposed to perform some I/O and terminate without producing a result will typically have type IO ()
. It can also be used when you need some uninteresting input; GHC has special syntax for this, but in Haskell 98 the way to mimic Lisp's cond
is like this:
case () of
() | c1 -> e1
| c2 -> e2
...
| otherwise -> e3
询问您拥有 ()
类型的值是完全有效的,但也非常无聊;你只能拥有一个合法的.
It's perfectly valid, but also perfectly boring, to ask what value of type ()
you have; there's only one legitimate one that you could have.
从Haskell-as-almost-category"的角度来看,()
是一个最终对象.也就是说,对于任何类型X
,恰好有一个类型为X->的合法函数.()
,即const()
.从另一个方向看,pigworker 提到的 Void
类型是一个初始对象.对于任何类型 X
,恰好有一个类型为 Void -> 的合法函数.X
,即荒谬
.如果您想进行分类推理,那么使用初始对象和最终对象会很有用.
From the "Haskell-as-almost-category" standpoint, ()
is a final object. That is, for any type X
, there is exactly one legitimate function of type X -> ()
, namely const ()
. From the other direction, the Void
type pigworker mentions is an initial object. For any type X
, there is exactly one legitimate function of type Void -> X
, namely absurd
. If you're in the mood for categorical reasoning, initial and final objects can be useful to have around.
这篇关于() 在 Haskell 中是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!