那就是定义。 Stream
类型定义了一个名为 :&
的单个中缀数据构造函数。 与...进行比较。
data Stream a = StreamCons a (Stream a)
这将定义相同类型,但创建StreamCons
而不是:&
作为数据构造函数。
中缀数据构造函数与常规中缀运算符不同,必须以冒号开头。
使用StreamCons
构造函数,您的constStream
函数将如下所示:
constStream :: a -> Stream a
-- constStream x = x :& (constStream x)
constStream x = StreamCons x (constStream x)
返回一个无限 列表 的相同函数看起来会像这样
constList :: a -> [a]
constList x = x : (constList x)
:&
的作用与 :
相同,但适用于 Stream a
而非 [a]
。实际上,Stream
和 []
之间唯一的区别在于 Stream a
仅包含表示 无限 的 a
序列的值,而 [a]
还包括有限的列表。