gpt4 book ai didi

haskell - 中缀数据构造运算符的解释

转载 作者:行者123 更新时间:2023-12-02 04:09:15 24 4
gpt4 key购买 nike

我正在阅读这篇有趣的博客文章,名为 Parse, don't validate其中包括:

To do this, we need a type that represents non-empty lists. Fortunately, the existing NonEmpty type from Data.List.NonEmpty is exactly that. It has the following definition:

data NonEmpty a = a :| [a]

Note that NonEmpty a is really just a tuple of an a and an ordinary, possibly-empty [a]. This conveniently models a non-empty list by storing the first element of the list separately from the list’s tail: even if the [a] component is [], the a component must always be present.

我能找到有关 :| 的唯一地方是在 this SO question 的评论中。 ,这意味着我需要对该问题/讨论有一些了解并解析格式。

有没有这方面的文档(我的搜索没有结果)并且有对其使用的简单解释吗?我不明白,因为我只是偶尔用 Haskell 浸入水中。

最佳答案

类型的全名是Data.List.NonEmpty .

您可以找到详细信息here

有网站https://hoogle.haskell.org/这是在 haskell 库中搜索函数、类型等的良好起点。

编辑:如果您与 :| 混淆那么这里有一些解释。

在 haskell 中,您可以定义自定义运算符,例如 . , $ , <*> 。同样,您可以使用自定义“名称”创建中缀构造函数。

所以你可以自己定义一个:

data MyType = Integer :++ Integer

并将它们用作

someVal :: MyType
someVal = 1 :++ 2

foo :: MyType -> Integer
foo (a :++ b) = a

请注意,它必须以 : 开头(感谢@Ben)

关于haskell - 中缀数据构造运算符的解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58760972/

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