: 中缀运算符在 Haskell 中有什么作用?
问题描述:
我正在阅读 Haskell 的温和介绍(不太温和)并且它重复使用 :
操作符而不直接解释它的作用.
I'm reading A Gentle Introduction to Haskell (which is not so gentle) and it repeatedly uses the :
operator without directly explaining what it does.
那么,它到底是做什么的?
So, what exactly does it do?
答
:
是前置"操作符:
x : xs
返回一个列表,其中 x
作为第一个元素,后跟 xs
中的所有元素.在其他函数式语言中,这通常称为 cons
,因为它通过从空列表中重复应用递归地构造"一个列表:
Returns a list which has x
as first element, followed by all elements in xs
. In other functional languages, this is usually called cons
, because it "cons"tructs a list recursively by repeated application from an empty list:
1 : 2 : 3 : 4 : []
是列表[1, 2, 3, 4]
.