[英]What does Cons and :-: mean in Haskell?
In LYAHFGG , one chapter says that list is defined as: 在LYAHFGG中 ,有一章说该列表定义为:
data List a = Cons a (List a) deriving (Show, Read, Eq, Ord)
I understand what most of this means apart from Cons. 我理解除了缺点之外,大多数这意味着什么。 When I try :t Cons
and :i Cons
in ghci
I get a not in scope error. 当我尝试:t Cons
和:i Cons
在ghci
:i Cons
我得到一个不在范围内的错误。 Later on in the chapter it also talks about :-: and how it's the same as Cons 在本章后面,它还讨论了: - 和它与Cons的相同之处
infixr 5 :-:
data List a = Empty | a :-: (List a) deriving (Show, Read, Eq, Ord)
But again I really don't understand what this :-:
means either. 但我真的不明白这是什么:-:
意味着。
In another resource, in a section about data types, they define the following data type: 在另一个资源中,在有关数据类型的部分中,它们定义以下数据类型:
data Expr = X
| Const Int
| Expr :+: Expr
| Expr :-: Expr
| Expr :*: Expr
| Expr :/: Expr
| IfZero Expr Expr Expr
deriving (Eq, Ord)
Where IfZero pqr
is the same as if p == 0 then q else r
. 其中IfZero pqr
与if p == 0 then q else r
相同, if p == 0 then q else r
。 Is this the same thing? 这是一回事吗? I'm mostly confused as to what the two :
s mean, and if it's mandatory syntax or just style choice. 我很困惑这两个:
s是什么意思,如果它是强制语法或只是样式选择。
data List a = Cons a (List a) deriving (Show, Read, Eq, Ord)
I understand what most of this means apart from Cons. 我理解除了缺点之外,大多数这意味着什么。 When I try
:t Cons
and:i Cons
inghci
I get a not in scope error. 当我尝试:t Cons
和:i Cons
在ghci
:i Cons
我得到一个不在范围内的错误。
You need to load the Haskell source file with the data
declaration before you can have Cons
in scope. 您需要使用data
声明加载Haskell源文件,然后才能在范围内使用Cons
。 Or, alternatively, you can enter that data
line directly in GHCi. 或者,您也可以直接在GHCi中输入该data
行。
For serious code, it's easier if you put it in a file and load it. 对于严肃的代码,如果将其放入文件并加载它会更容易。 This is because the learning process typically involves modifying the file a bit, reloading it, trying some test in GHCi, modifying the file again, etc. Doing this in GHCi is cumbersome. 这是因为学习过程通常涉及稍微修改文件,重新加载文件,在GHCi中尝试一些测试,再次修改文件等。在GHCi中执行此操作非常麻烦。
Anyway, Cons
is just the constructor name -- it is an arbitrary name. 无论如何, Cons
只是构造函数名称 - 它是一个任意名称。 You can use data List a = Foobar a (List a) ....
and name it Foobar
, if you wish. 您可以使用data List a = Foobar a (List a) ....
并将其命名为Foobar
,如果您愿意的话。 Cons
is a historic name, though, originating from Lisp. 但是, Cons
是一个历史名称,源自Lisp。
:-:
is another arbitrary name for the constructor, except that it can be used infix . :-:
是构造函数的另一个任意名称,除了它可以使用中缀 。 Ie instead of Cons 1 someList
one can write 1 :-: someList
. 即,而不是Cons 1 someList
可以写1 :-: someList
。
:-:
is just an infix name for a data constructor. :-:
只是数据构造函数的中缀名称。 You could see that data
declaration as equivalent to 你可以看到data
声明等同于
data List a = Empty | (:-:) a (List a)
Semantically, there is no difference between using (:-:)
or Cons
, but it's much nicer to read 从语义上讲,使用(:-:)
或Cons
之间没有区别,但阅读起来要好得多
1 :-: 2 :-: 3 :-: 4 :-: Empty
than either 比任何一个
Cons 1 (Cons 2 (Cons 3 (Cons 4 Empty)))
or 要么
1 `Cons` (2 `Cons` (3 `Cons` (4 `Cons` Empty)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.