gpt4 book ai didi

haskell - Haskell 中带 & 符号的冒号的含义 (:&)

转载 作者:行者123 更新时间:2023-12-02 13:28:19 27 4
gpt4 key购买 nike

实现来自Haskell book (author Anton Kholomiov)的流(第 70 页)

data Stream a = a :& Stream a

我明白:&的含义,但找不到它的定义

最佳答案

这就是定义。 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 仅包含表示无限 序列的值。 as,而[a]也包含有限列表。

关于haskell - Haskell 中带 & 符号的冒号的含义 (:&),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40244010/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com