从Haskell中的列表中删除共轭元组(Removing conjugate tuples from a list in Haskell)

编程入门 行业动态 更新时间:2024-10-27 20:27:21
从Haskell中的列表中删除共轭元组(Removing conjugate tuples from a list in Haskell)

我有一个元组列表。 每个元组也是一个元组。 对于每个外层元组,我有一个共轭元组。 我的意思是,如果我在列表中有(a,b),那么我也有(b,a)。 我想要的是从列表中删除共轭元组。

为了给你一个我的清单的具体例子,它是:

[((1,2,1,0),(5,2,1,0)),((2,4,4,0),(2,5,4,0)),((2,5,4,0),(2,4,4,0)),((3,1,1,0),(3,3,2,0)),((3,3,2,0),(3,1,1,0)),((5,1,7,0),(8,1,7,0)),((5,2,1,0),(1,2,1,0)),((5,5,8,0),(8,5,8,0)),((5,6,6,0),(8,6,8,0)),((5,9,9,0),(8,9,9,0)),((6,3,5,0),(6,8,9,0)),((6,8,9,0),(6,3,5,0)),((7,7,6,0),(9,7,6,0)),((8,1,7,0),(5,1,7,0)),((8,5,8,0),(5,5,8,0)),((8,6,8,0),(5,6,6,0)),((8,9,9,0),(5,9,9,0)),((9,7,6,0),(7,7,6,0))]

去除共轭元组后的结果应该是:

[((1,2,1,0),(5,2,1,0)),((2,4,4,0),(2,5,4,0)),((3,1,1,0),(3,3,2,0)),((5,1,7,0),(8,1,7,0)),((5,5,8,0),(8,5,8,0)),((5,6,6,0),(8,6,8,0)),((5,9,9,0),(8,9,9,0)),((6,3,5,0),(6,8,9,0)),((7,7,6,0),(9,7,6,0)),]

我尝试了几个小时没有成功。 任何帮助将不胜感激。

I have a list of tuples. Each tuple is a tuple as well. For each outer tuple, I have a conjugate tuple. By this I mean that if I have (a,b) in the list then I also have (b,a) as well. What I want is to remove the conjugate tuples from the list.

To give you a concrete example of my list here it is:

[((1,2,1,0),(5,2,1,0)),((2,4,4,0),(2,5,4,0)),((2,5,4,0),(2,4,4,0)),((3,1,1,0),(3,3,2,0)),((3,3,2,0),(3,1,1,0)),((5,1,7,0),(8,1,7,0)),((5,2,1,0),(1,2,1,0)),((5,5,8,0),(8,5,8,0)),((5,6,6,0),(8,6,8,0)),((5,9,9,0),(8,9,9,0)),((6,3,5,0),(6,8,9,0)),((6,8,9,0),(6,3,5,0)),((7,7,6,0),(9,7,6,0)),((8,1,7,0),(5,1,7,0)),((8,5,8,0),(5,5,8,0)),((8,6,8,0),(5,6,6,0)),((8,9,9,0),(5,9,9,0)),((9,7,6,0),(7,7,6,0))]

The result after removing the conjugate tuples should be:

[((1,2,1,0),(5,2,1,0)),((2,4,4,0),(2,5,4,0)),((3,1,1,0),(3,3,2,0)),((5,1,7,0),(8,1,7,0)),((5,5,8,0),(8,5,8,0)),((5,6,6,0),(8,6,8,0)),((5,9,9,0),(8,9,9,0)),((6,3,5,0),(6,8,9,0)),((7,7,6,0),(9,7,6,0)),]

I have tried for hours with no success. Any help will be appreciated.

最满意答案

“元组的每个元素也是一个元组”

removeConj xs = foldl f [] xs where f b a = if (elem (snd a, fst a) b) then b else (a:b)

"Each element of the tuple is a tuple as well"

removeConj xs = foldl f [] xs where f b a = if (elem (snd a, fst a) b) then b else (a:b)

更多推荐

本文发布于:2023-07-28 23:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1310284.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:共轭   列表中   Haskell   Removing   list

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!