Mathmeeeeen
Mathmeeeeen

Reputation: 159

Accessing certain elements of tuples

I have to solve a problem for university studies. So what I want basically is that I want to access certain elements two 2-tuples. I already defined a function:

taxiDistance :: (Integer, Integer) -> (Integer, Integer) -> Integer

as you can see the function takes two 2-tuples containing integers and returns a integer. And now I have to add the first elements of both tuples and 2nd of both. And I don't now how to access these values which were put in by a user before.

Thank you for your help.

Upvotes: 0

Views: 137

Answers (2)

cmdv
cmdv

Reputation: 1743

you can use the functions fst + snd like this:

taxiDistance :: (Integer, Integer) -> (Integer, Integer) -> Integer
taxiDistance x y = fst x + fst y

or alternatively you can deconstruct the tuples in the declaration like:

taxiDistance :: (Integer, Integer) -> (Integer, Integer) -> Integer
taxiDistance (a,b) (c,d) = a + b + c + d

Upvotes: 4

tejasbubane
tejasbubane

Reputation: 942

Since this is university studies I won't give the entire answer right away but will point you to read up on pattern matching and tuple constructor.

Upvotes: 1

Related Questions