使用语法糖检查Haskell中集合中的所有元素 [英] Checking for all Elements in a Set in Haskell using syntactic sugar

查看:52
本文介绍了使用语法糖检查Haskell中集合中的所有元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试删除(String,Int)列表的整数重复项,在此确保没有 String 重复项.

I try to remove the Integer duplicates of a List of (String, Int), where I am guaranteed that there is no String duplicate.

是否可以在Haskell中评估类似的内容:

Is it possible to evaluate something like this in Haskell:

我尝试过:

[(a,b) | (a,b) <- bs, (c,k) <- bs, ((k == b) <= (a == c))]

但这还行不通.

我很清楚,您可以使用更复杂的语法来实现.例如,通过递归搜索列表中的每个元素重复项...

I am well aware, that you can achieve that using more complex syntax. For example by recursively searching the List for each elements duplicates...

推荐答案

(注意:这是此答案的全新版本.以前的版本完全不合标准.)

(NB: this is a completely new version of this answer. Previous was totally off-base.)

要更紧密地跟踪您的数学集合理解,我们可以将答案中的定义调​​整为

To follow your mathematical set comprehension more closely, we can tweak the definition in your answer as

uniquesOnly :: (Eq a, Eq b) => [(a, b)] -> [(a, b)]
uniquesOnly bs = 
   [(a,b) | (a,b) <- bs, 
            [(c,d) | (c,d) <- bs, d == b] ==
            [(a,d) | (c,d) <- bs, d == b]]

",用于bs中的所有(c,d),以使d == b跟随c == a".

"for all (c,d) in bs such that d==b it follows c==a".

uniquesOnly [[1,1),(2,2),(3,1)] 返回 [(2,2)] .

这篇关于使用语法糖检查Haskell中集合中的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆