Change the entries in an array in Haskell









up vote
0
down vote

favorite












Suppose you have



let a = array ((1,1),(2,2)) [((2,1),3),((1,2),2),((1,1),2),((2,2),3)]



Now I want that to multiply the 3 in the last tuple with some number. How can I do that?










share|improve this question

















  • 2




    what did you try?
    – OznOg
    Nov 10 at 20:22










  • Which array function is this?
    – melpomene
    Nov 10 at 20:25










  • I tried to make an accumArray, but that didn't help much. I also tried with product(snd (unzip (assocs a))) but then I don't know how to put the result in the array.
    – Dreikäsehoch
    Nov 10 at 20:27










  • @melpomene the array from Data.Array, i guess
    – Dreikäsehoch
    Nov 10 at 20:30










  • If you want to modify entries, why are you using an array?
    – melpomene
    Nov 10 at 20:37














up vote
0
down vote

favorite












Suppose you have



let a = array ((1,1),(2,2)) [((2,1),3),((1,2),2),((1,1),2),((2,2),3)]



Now I want that to multiply the 3 in the last tuple with some number. How can I do that?










share|improve this question

















  • 2




    what did you try?
    – OznOg
    Nov 10 at 20:22










  • Which array function is this?
    – melpomene
    Nov 10 at 20:25










  • I tried to make an accumArray, but that didn't help much. I also tried with product(snd (unzip (assocs a))) but then I don't know how to put the result in the array.
    – Dreikäsehoch
    Nov 10 at 20:27










  • @melpomene the array from Data.Array, i guess
    – Dreikäsehoch
    Nov 10 at 20:30










  • If you want to modify entries, why are you using an array?
    – melpomene
    Nov 10 at 20:37












up vote
0
down vote

favorite









up vote
0
down vote

favorite











Suppose you have



let a = array ((1,1),(2,2)) [((2,1),3),((1,2),2),((1,1),2),((2,2),3)]



Now I want that to multiply the 3 in the last tuple with some number. How can I do that?










share|improve this question













Suppose you have



let a = array ((1,1),(2,2)) [((2,1),3),((1,2),2),((1,1),2),((2,2),3)]



Now I want that to multiply the 3 in the last tuple with some number. How can I do that?







arrays haskell






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 20:18









Dreikäsehoch

1135




1135







  • 2




    what did you try?
    – OznOg
    Nov 10 at 20:22










  • Which array function is this?
    – melpomene
    Nov 10 at 20:25










  • I tried to make an accumArray, but that didn't help much. I also tried with product(snd (unzip (assocs a))) but then I don't know how to put the result in the array.
    – Dreikäsehoch
    Nov 10 at 20:27










  • @melpomene the array from Data.Array, i guess
    – Dreikäsehoch
    Nov 10 at 20:30










  • If you want to modify entries, why are you using an array?
    – melpomene
    Nov 10 at 20:37












  • 2




    what did you try?
    – OznOg
    Nov 10 at 20:22










  • Which array function is this?
    – melpomene
    Nov 10 at 20:25










  • I tried to make an accumArray, but that didn't help much. I also tried with product(snd (unzip (assocs a))) but then I don't know how to put the result in the array.
    – Dreikäsehoch
    Nov 10 at 20:27










  • @melpomene the array from Data.Array, i guess
    – Dreikäsehoch
    Nov 10 at 20:30










  • If you want to modify entries, why are you using an array?
    – melpomene
    Nov 10 at 20:37







2




2




what did you try?
– OznOg
Nov 10 at 20:22




what did you try?
– OznOg
Nov 10 at 20:22












Which array function is this?
– melpomene
Nov 10 at 20:25




Which array function is this?
– melpomene
Nov 10 at 20:25












I tried to make an accumArray, but that didn't help much. I also tried with product(snd (unzip (assocs a))) but then I don't know how to put the result in the array.
– Dreikäsehoch
Nov 10 at 20:27




I tried to make an accumArray, but that didn't help much. I also tried with product(snd (unzip (assocs a))) but then I don't know how to put the result in the array.
– Dreikäsehoch
Nov 10 at 20:27












@melpomene the array from Data.Array, i guess
– Dreikäsehoch
Nov 10 at 20:30




@melpomene the array from Data.Array, i guess
– Dreikäsehoch
Nov 10 at 20:30












If you want to modify entries, why are you using an array?
– melpomene
Nov 10 at 20:37




If you want to modify entries, why are you using an array?
– melpomene
Nov 10 at 20:37












3 Answers
3






active

oldest

votes

















up vote
5
down vote



accepted










If you wanted to multiply that by 5 it would be:



accum (*) a [((2,2),5)]
-- ^ ^ ^ ^
-- function to combine values
-- array to read
-- one of the indices to manipulated
-- value to give to f for the associated index


The signature of this function reads



accum :: Ix i => (e -> a -> e) -> Array i e -> [(i, a)] -> Array i e


and its documentation says:




accum f takes an array and an association list and accumulates pairs
from the list into the array with the accumulating function f.




So calling accum f arrayValue listWithPairsOfIndicesAndValues will call f for every index given in listWithPairsOfIndicesAndValues providing the old value and the value from listWithPairsOfIndicesAndValues for this index and return a new array with all the positions mentioned in listWithPairsOfIndicesAndValues updated with the values returned by the respective calls to f.






share|improve this answer





























    up vote
    4
    down vote













    There's two different ways to your goal:




    1. Using the incremental update:



      a // [((2,2),(3*7))]



      >>> array ((1,1),(2,2)) [((1,1),2),((1,2),2),((2,1),3),((2,2),21)]



    (Instead of 3*7, you could use your own n and refer to that same location (a!(2,2)))




    1. Using the accumulation:



      accum (*) a [((2,2), 7)]



      >>> array ((1,1),(2,2)) [((1,1),2),((1,2),2),((2,1),3),((2,2),21)]







    share|improve this answer



























      up vote
      2
      down vote













      One could use a // [(2,2), n * (a ! (2,2))] to multiply the element with index (2,2) by n.



      (There should also be some lens-y approach with a better syntax)






      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%2f53243052%2fchange-the-entries-in-an-array-in-haskell%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        5
        down vote



        accepted










        If you wanted to multiply that by 5 it would be:



        accum (*) a [((2,2),5)]
        -- ^ ^ ^ ^
        -- function to combine values
        -- array to read
        -- one of the indices to manipulated
        -- value to give to f for the associated index


        The signature of this function reads



        accum :: Ix i => (e -> a -> e) -> Array i e -> [(i, a)] -> Array i e


        and its documentation says:




        accum f takes an array and an association list and accumulates pairs
        from the list into the array with the accumulating function f.




        So calling accum f arrayValue listWithPairsOfIndicesAndValues will call f for every index given in listWithPairsOfIndicesAndValues providing the old value and the value from listWithPairsOfIndicesAndValues for this index and return a new array with all the positions mentioned in listWithPairsOfIndicesAndValues updated with the values returned by the respective calls to f.






        share|improve this answer


























          up vote
          5
          down vote



          accepted










          If you wanted to multiply that by 5 it would be:



          accum (*) a [((2,2),5)]
          -- ^ ^ ^ ^
          -- function to combine values
          -- array to read
          -- one of the indices to manipulated
          -- value to give to f for the associated index


          The signature of this function reads



          accum :: Ix i => (e -> a -> e) -> Array i e -> [(i, a)] -> Array i e


          and its documentation says:




          accum f takes an array and an association list and accumulates pairs
          from the list into the array with the accumulating function f.




          So calling accum f arrayValue listWithPairsOfIndicesAndValues will call f for every index given in listWithPairsOfIndicesAndValues providing the old value and the value from listWithPairsOfIndicesAndValues for this index and return a new array with all the positions mentioned in listWithPairsOfIndicesAndValues updated with the values returned by the respective calls to f.






          share|improve this answer
























            up vote
            5
            down vote



            accepted







            up vote
            5
            down vote



            accepted






            If you wanted to multiply that by 5 it would be:



            accum (*) a [((2,2),5)]
            -- ^ ^ ^ ^
            -- function to combine values
            -- array to read
            -- one of the indices to manipulated
            -- value to give to f for the associated index


            The signature of this function reads



            accum :: Ix i => (e -> a -> e) -> Array i e -> [(i, a)] -> Array i e


            and its documentation says:




            accum f takes an array and an association list and accumulates pairs
            from the list into the array with the accumulating function f.




            So calling accum f arrayValue listWithPairsOfIndicesAndValues will call f for every index given in listWithPairsOfIndicesAndValues providing the old value and the value from listWithPairsOfIndicesAndValues for this index and return a new array with all the positions mentioned in listWithPairsOfIndicesAndValues updated with the values returned by the respective calls to f.






            share|improve this answer














            If you wanted to multiply that by 5 it would be:



            accum (*) a [((2,2),5)]
            -- ^ ^ ^ ^
            -- function to combine values
            -- array to read
            -- one of the indices to manipulated
            -- value to give to f for the associated index


            The signature of this function reads



            accum :: Ix i => (e -> a -> e) -> Array i e -> [(i, a)] -> Array i e


            and its documentation says:




            accum f takes an array and an association list and accumulates pairs
            from the list into the array with the accumulating function f.




            So calling accum f arrayValue listWithPairsOfIndicesAndValues will call f for every index given in listWithPairsOfIndicesAndValues providing the old value and the value from listWithPairsOfIndicesAndValues for this index and return a new array with all the positions mentioned in listWithPairsOfIndicesAndValues updated with the values returned by the respective calls to f.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 10 at 20:52

























            answered Nov 10 at 20:46









            typetetris

            2,615323




            2,615323






















                up vote
                4
                down vote













                There's two different ways to your goal:




                1. Using the incremental update:



                  a // [((2,2),(3*7))]



                  >>> array ((1,1),(2,2)) [((1,1),2),((1,2),2),((2,1),3),((2,2),21)]



                (Instead of 3*7, you could use your own n and refer to that same location (a!(2,2)))




                1. Using the accumulation:



                  accum (*) a [((2,2), 7)]



                  >>> array ((1,1),(2,2)) [((1,1),2),((1,2),2),((2,1),3),((2,2),21)]







                share|improve this answer
























                  up vote
                  4
                  down vote













                  There's two different ways to your goal:




                  1. Using the incremental update:



                    a // [((2,2),(3*7))]



                    >>> array ((1,1),(2,2)) [((1,1),2),((1,2),2),((2,1),3),((2,2),21)]



                  (Instead of 3*7, you could use your own n and refer to that same location (a!(2,2)))




                  1. Using the accumulation:



                    accum (*) a [((2,2), 7)]



                    >>> array ((1,1),(2,2)) [((1,1),2),((1,2),2),((2,1),3),((2,2),21)]







                  share|improve this answer






















                    up vote
                    4
                    down vote










                    up vote
                    4
                    down vote









                    There's two different ways to your goal:




                    1. Using the incremental update:



                      a // [((2,2),(3*7))]



                      >>> array ((1,1),(2,2)) [((1,1),2),((1,2),2),((2,1),3),((2,2),21)]



                    (Instead of 3*7, you could use your own n and refer to that same location (a!(2,2)))




                    1. Using the accumulation:



                      accum (*) a [((2,2), 7)]



                      >>> array ((1,1),(2,2)) [((1,1),2),((1,2),2),((2,1),3),((2,2),21)]







                    share|improve this answer












                    There's two different ways to your goal:




                    1. Using the incremental update:



                      a // [((2,2),(3*7))]



                      >>> array ((1,1),(2,2)) [((1,1),2),((1,2),2),((2,1),3),((2,2),21)]



                    (Instead of 3*7, you could use your own n and refer to that same location (a!(2,2)))




                    1. Using the accumulation:



                      accum (*) a [((2,2), 7)]



                      >>> array ((1,1),(2,2)) [((1,1),2),((1,2),2),((2,1),3),((2,2),21)]








                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 10 at 20:51









                    RoyM

                    746




                    746




















                        up vote
                        2
                        down vote













                        One could use a // [(2,2), n * (a ! (2,2))] to multiply the element with index (2,2) by n.



                        (There should also be some lens-y approach with a better syntax)






                        share|improve this answer
























                          up vote
                          2
                          down vote













                          One could use a // [(2,2), n * (a ! (2,2))] to multiply the element with index (2,2) by n.



                          (There should also be some lens-y approach with a better syntax)






                          share|improve this answer






















                            up vote
                            2
                            down vote










                            up vote
                            2
                            down vote









                            One could use a // [(2,2), n * (a ! (2,2))] to multiply the element with index (2,2) by n.



                            (There should also be some lens-y approach with a better syntax)






                            share|improve this answer












                            One could use a // [(2,2), n * (a ! (2,2))] to multiply the element with index (2,2) by n.



                            (There should also be some lens-y approach with a better syntax)







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 10 at 20:48









                            chi

                            71.8k279132




                            71.8k279132



























                                 

                                draft saved


                                draft discarded















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243052%2fchange-the-entries-in-an-array-in-haskell%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







                                Popular posts from this blog

                                Top Tejano songwriter Luis Silva dead of heart attack at 64

                                政党

                                天津地下鉄3号線