简体   繁体   English

a : -> b 在 Haskell 中是什么意思?

[英]What does a : -> b mean in Haskell?

I am trying to implement a dictionary in Haskell and I see that the data type must be我正在尝试在 Haskell 中实现一个字典,我看到数据类型必须是

data Rel a b = a :-> b

but I don't see what it means, I think it's the same thing as key-value or similar但我不明白这是什么意思,我认为它与键值或类似的东西是一样的

Looks like :-> is just a fancy constructor name:看起来:->只是一个花哨的构造函数名称:

Prelude> data Rel a b = a :-> b
Prelude> :t (5 :-> 6)
(5 :-> 6) :: (Num a, Num b) => Rel a b

In this code, 5 :-> 6 produces a Rel value.在这段代码中, 5 :-> 6产生一个Rel值。

One could've used data Rel ab = a :-% b instead, for example, where :-% would be the constructor name.例如,可以使用data Rel ab = a :-% b代替,其中:-%将是构造函数名称。

This declaration defines what :-> means: it's an infix data constructor.此声明定义:->含义:它是一个中缀数据构造函数。

> :t 3 :-> 5
3 :-> 5 :: (Num a, Num b) => Rel a b

The definition could also have been written定义也可以写成

data Rel a b = (:->) a b

in the usual prefix notation.在通常的前缀符号中。

It's a lot like an ordinary infix operator, except an infix data constructor must start with a : .它很像普通的中缀运算符,只是中缀数据构造函数必须:开头。 You may have seen the pseudo-Haskell definition of the list type:您可能已经看过列表类型的伪 Haskell 定义:

data [] a = [] | a : [a]

which defines (:) as the infix data constructor for non-empty lists.它将(:)定义为非空列表的中缀数据构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM