简体   繁体   中英

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 :

[(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.

if someone has an idea how to write in 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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