简体   繁体   中英

List operation in Haskell

I have this list of type ([(Double,Double)],[(Double,Double)]) . example list = ([(1.0,1.0), (2.0,1.0), (1.0,1.0), (1.0,3.0)],[(1.0,4.0), (1.0,5.0), (1.0,1.0), (1.0,2.0), (1.0,3.0), (1.0,4.0), (1.0,5.0)])

How would I access all the data after the fourth tuple (1.0, 3.0). I have already tried the tail function but doesn't seem to work. Thanks.

Well, for one, your list isn't a list, but a tuple :)

type MyData = (MyList, MyList)
type MyList = [MyListElem]
type MyListElem = (Double, Double)

Now, accessing the 2nd list is simply snd .

snd :: (a,b) -> b

So in your case:

snd :: MyData -> MyList

Alternatively, using Lens , you can use a lens on that directly:

list ^. _2

It's not a list, but a tuple of lists. In fact, a tuple of lists of tuples.

To get the second part of a tuple, use the snd command:

snd ([(1.0,1.0), (2.0,1.0), (1.0,1.0), (1.0,3.0)],[(1.0,4.0), (1.0,5.0), (1.0,1.0), (1.0,2.0), (1.0,3.0), (1.0,4.0), (1.0,5.0)])

This yields:

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

From here on, you can continue to get the parts of the second list using tail or the !! operator.

For completeness, the first part of a tuple can be obtained using the fst command.

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