我似乎无法理解“列表差异”; (\\) 操作员
我在 Haskell 中听说过术语“列表差异”(\\)
运算符,但仍然不太知道如何理解它。有什么例子或想法吗?
I have heard the term 'list difference' (\\)
operator in Haskell but still don't quite know how to get my head around it. Any examples or ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
( \\)
运算符(和差异 function
) 实现 设置差异,所以,如果您有两个列表,
a
和b
,它仅返回a
中不在b
中的元素,如图所示:The
(\\)
operator (and thedifference
function) implements set difference, so, if you have two lists,a
andb
, it returns only those elements ofa
that are not inb
, as illustrated:简而言之,它需要两个列表,遍历第二个列表,然后对于每个项目,从第一个列表中删除同一项目的第一个实例。
Simply put, it takes two lists, goes through the second and for each item, removes the first instance of the same item from the first list.
xs \\ ys
是xs
中不在ys
中的所有元素。也许列表理解会澄清这一点:或者,如果你可以在 Haskell 中做到这一点,
这来自 集合论 的设置差异。基本思想是,您正在从一个元素集合中“减去”另一个元素集合,因此出现了术语“差异”。
xs \\ ys
is all the elements inxs
that are not inys
. Maybe a list comprehension will clarify this:or, if you could do this in Haskell,
This comes from set theory's set difference. The basic idea is that you are "subtracting" one collection of elements from another, hence the term "difference".
假设您有一个事物列表,例如城市。让我们以这个列表为例:
现在您想要删除欧洲的所有城市。您知道,这些城市位于欧洲:
要获取
a
中不在欧洲的城市列表,以便不在b
中,您可以使用 <代码>(\\):Suppose, you have a list of things, for example cities. Let's take for instance this list:
Now you want to remove all cities that are in Europe. You know, that those cities are in Europe:
To get a list of cities in
a
, that are not in Europe, so that are not inb
, you can use(\\)
: