我有这样的元组列表:
[(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)]
其中第一个元素是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)]
它从第一个元组获取第一个元素,从第二个元组获取第一个元素,如果它们不相等,则Y不变,但如果它们相等,我们将一个添加到Y.
如果有人知道如何在Haskell中写作?
答案 0 :(得分:-1)
未经测试,但这样的事情会发生:
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)