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.










share|improve this question






















  • if you're adding the first and second elements you're probably doing the computations wrong.
    – karakfa
    Nov 7 at 17:24















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.










share|improve this question






















  • if you're adding the first and second elements you're probably doing the computations wrong.
    – karakfa
    Nov 7 at 17:24













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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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


















  • 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












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





share|improve this answer





















  • 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


















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.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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

























    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





    share|improve this answer





















    • 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















    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





    share|improve this answer





















    • 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













    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





    share|improve this answer












    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






    share|improve this answer












    share|improve this answer



    share|improve this answer










    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


















    • 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












    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.






    share|improve this answer

























      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.






      share|improve this answer























        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 7 at 17:06









        tejasbubane

        51637




        51637






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            這個網誌中的熱門文章

            Hercules Kyvelos

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud