简体   繁体   English

如何比较列表中的两个元组?

[英]How to compare two tuple in list?

I have list of tuples like this: 我有这样的元组列表:

[(0.0,0.0),(1.0,0.0),(2.0,0.0),(3.0,0.0),(3.0,0.0),(3.0,0.0),(4.0,0.0),(4.0,0.0)]

where first element is the X-coordinate and second the Y-coordinate and I now want to write function that will return : 其中第一个元素是X坐标,第二个元素是Y坐标,我现在想编写将返回的函数:

[(0.0,0.0),(1.0,0.0),(2.0,0.0),(3.0,0.0),(3.0,1.0),(3.0,2.0),(4.0,0.0),(4.0,1.0)]

It takes first element from first tuple and first from second tuple and if they are not equal, Y is unchanged but if they are equal we add one to Y. 它从第一个元组中取出第一个元素,从第二个元组中取出第一个元素,如果它们不相等,则Y不变,但如果它们相等,则将Y加一个。

if someone has an idea how to write in Haskell? 如果有人有想法如何在Haskell中写作?

Not tested, but something like this will do: 尚未测试,但可以执行以下操作:

foo :: [(Float, Float)] -> [(Float, Float)]
foo []  = []
foo [x] = [x]
foo ((xx, xy):(yx, yy):xs) = (xx, xy): theTuple : (foo ((yx, yy):xs))
    where
        theTuple = if xx == yx then (yx, yy) else (yx, yy+1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM