()在Haskell中意味着什么 [英] What does () mean in Haskell

查看:108
本文介绍了()在Haskell中意味着什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  put :: s  - >在一些Haskell代码中, m()

()这意味着什么?



我会使用搜索引擎,但找不到一个处理()如果不是奇怪的特殊语法,它可以被定义为
$ b

解决方案

$ b

  data()=()

关于最无聊的类型。语法应该让你想到元组:(a,b)是一对,(a,b,c)是一个三元组等,并且()是一个0元组。唯一缺少的是一个1元组,它不能使用该语法,因为它会与通常使用的括号发生冲突。



( )经常被用作没有有趣结果的结果。例如,应该执行某些I / O并终止而不产生结果的 IO 操作通常具有类型 IO()。当你需要一些无趣的输入时也可以使用它; GHC具有特殊的语法,但在Haskell 98中,模仿Lisp的 cond 的方式如下所示:

<$ p $ $> $ b $()|的情况() c1 - > e1
| c2 - > e2
...
|否则 - > e3

这是完全有效的,但也很无聊,询问()你有;



从Haskell-as-almost-category的角度来看,()是一个最终对象。也就是说,对于任何类型的 X ,都有正好一个类型的合法函数 X - > (),即 const()。从另一个方向看, Void 类型的pigworker提到的是一个初始对象。对于任何类型的 X ,都有 类型的合法函数 Void - > X ,即荒谬。如果你对分类推理有兴趣,初始和最终对象可能会有用。


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 () = ()

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.

() 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.

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中意味着什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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