Accessing certain elements of tuples
up vote
0
down vote
favorite
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.
haskell tuples element
add a comment |
up vote
0
down vote
favorite
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.
haskell tuples element
if you're adding the first and second elements you're probably doing the computations wrong.
– karakfa
Nov 7 at 17:24
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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.
haskell tuples element
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.
haskell tuples element
haskell tuples element
asked Nov 7 at 16:58
Mathmeeeeen
111
111
if you're adding the first and second elements you're probably doing the computations wrong.
– karakfa
Nov 7 at 17:24
add a comment |
if you're adding the first and second elements you're probably doing the computations wrong.
– karakfa
Nov 7 at 17:24
if you're adding the first and second elements you're probably doing the computations wrong.
– karakfa
Nov 7 at 17:24
if you're adding the first and second elements you're probably doing the computations wrong.
– karakfa
Nov 7 at 17:24
add a comment |
2 Answers
2
active
oldest
votes
up vote
4
down vote
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
damn I ruthless just came out with the answer!! sorry :S
– cmdv
Nov 7 at 17:20
I think this is fine, I don't see how it's pedagogically helpful not to reveal the syntax of what they're trying to do. There's nothing to "think through" here.
– luqui
Nov 7 at 19:58
This is fine as it is. You didn't solve the full task of the OP: the OP also needs to compute the taxi distance, and that will involve a (slightly) more complex formula. Revealing "how to access the pair components" is perfectly OK, since you can find it on books anyway. What is usually frowned upon (IMO) is to answer a much more broad question dumping the full solution, without a careful explanation. Doing that would only allow the OP to copy&paste their homework, with no chance of actually learning anything. But you didn't do that :)
– chi
Nov 7 at 21:52
add a comment |
up vote
1
down vote
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.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
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
damn I ruthless just came out with the answer!! sorry :S
– cmdv
Nov 7 at 17:20
I think this is fine, I don't see how it's pedagogically helpful not to reveal the syntax of what they're trying to do. There's nothing to "think through" here.
– luqui
Nov 7 at 19:58
This is fine as it is. You didn't solve the full task of the OP: the OP also needs to compute the taxi distance, and that will involve a (slightly) more complex formula. Revealing "how to access the pair components" is perfectly OK, since you can find it on books anyway. What is usually frowned upon (IMO) is to answer a much more broad question dumping the full solution, without a careful explanation. Doing that would only allow the OP to copy&paste their homework, with no chance of actually learning anything. But you didn't do that :)
– chi
Nov 7 at 21:52
add a comment |
up vote
4
down vote
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
damn I ruthless just came out with the answer!! sorry :S
– cmdv
Nov 7 at 17:20
I think this is fine, I don't see how it's pedagogically helpful not to reveal the syntax of what they're trying to do. There's nothing to "think through" here.
– luqui
Nov 7 at 19:58
This is fine as it is. You didn't solve the full task of the OP: the OP also needs to compute the taxi distance, and that will involve a (slightly) more complex formula. Revealing "how to access the pair components" is perfectly OK, since you can find it on books anyway. What is usually frowned upon (IMO) is to answer a much more broad question dumping the full solution, without a careful explanation. Doing that would only allow the OP to copy&paste their homework, with no chance of actually learning anything. But you didn't do that :)
– chi
Nov 7 at 21:52
add a comment |
up vote
4
down vote
up vote
4
down vote
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
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
answered Nov 7 at 17:07
cmdv
8841919
8841919
damn I ruthless just came out with the answer!! sorry :S
– cmdv
Nov 7 at 17:20
I think this is fine, I don't see how it's pedagogically helpful not to reveal the syntax of what they're trying to do. There's nothing to "think through" here.
– luqui
Nov 7 at 19:58
This is fine as it is. You didn't solve the full task of the OP: the OP also needs to compute the taxi distance, and that will involve a (slightly) more complex formula. Revealing "how to access the pair components" is perfectly OK, since you can find it on books anyway. What is usually frowned upon (IMO) is to answer a much more broad question dumping the full solution, without a careful explanation. Doing that would only allow the OP to copy&paste their homework, with no chance of actually learning anything. But you didn't do that :)
– chi
Nov 7 at 21:52
add a comment |
damn I ruthless just came out with the answer!! sorry :S
– cmdv
Nov 7 at 17:20
I think this is fine, I don't see how it's pedagogically helpful not to reveal the syntax of what they're trying to do. There's nothing to "think through" here.
– luqui
Nov 7 at 19:58
This is fine as it is. You didn't solve the full task of the OP: the OP also needs to compute the taxi distance, and that will involve a (slightly) more complex formula. Revealing "how to access the pair components" is perfectly OK, since you can find it on books anyway. What is usually frowned upon (IMO) is to answer a much more broad question dumping the full solution, without a careful explanation. Doing that would only allow the OP to copy&paste their homework, with no chance of actually learning anything. But you didn't do that :)
– chi
Nov 7 at 21:52
damn I ruthless just came out with the answer!! sorry :S
– cmdv
Nov 7 at 17:20
damn I ruthless just came out with the answer!! sorry :S
– cmdv
Nov 7 at 17:20
I think this is fine, I don't see how it's pedagogically helpful not to reveal the syntax of what they're trying to do. There's nothing to "think through" here.
– luqui
Nov 7 at 19:58
I think this is fine, I don't see how it's pedagogically helpful not to reveal the syntax of what they're trying to do. There's nothing to "think through" here.
– luqui
Nov 7 at 19:58
This is fine as it is. You didn't solve the full task of the OP: the OP also needs to compute the taxi distance, and that will involve a (slightly) more complex formula. Revealing "how to access the pair components" is perfectly OK, since you can find it on books anyway. What is usually frowned upon (IMO) is to answer a much more broad question dumping the full solution, without a careful explanation. Doing that would only allow the OP to copy&paste their homework, with no chance of actually learning anything. But you didn't do that :)
– chi
Nov 7 at 21:52
This is fine as it is. You didn't solve the full task of the OP: the OP also needs to compute the taxi distance, and that will involve a (slightly) more complex formula. Revealing "how to access the pair components" is perfectly OK, since you can find it on books anyway. What is usually frowned upon (IMO) is to answer a much more broad question dumping the full solution, without a careful explanation. Doing that would only allow the OP to copy&paste their homework, with no chance of actually learning anything. But you didn't do that :)
– chi
Nov 7 at 21:52
add a comment |
up vote
1
down vote
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.
add a comment |
up vote
1
down vote
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.
add a comment |
up vote
1
down vote
up vote
1
down vote
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.
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.
answered Nov 7 at 17:06
tejasbubane
51637
51637
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53194251%2faccessing-certain-elements-of-tuples%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
if you're adding the first and second elements you're probably doing the computations wrong.
– karakfa
Nov 7 at 17:24