Sunil Gautam
Sunil Gautam

Reputation: 49

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.

Upvotes: 1

Views: 158

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

Upvotes: 1

Bartek Banachewicz
Bartek Banachewicz

Reputation: 39390

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

Upvotes: 4

Related Questions