<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <div class="moz-cite-prefix"><br>
      I know it's doable. I was asking if there's a practical / elegant&nbsp;
      way to do it. <br>
      I see a lot of Haskell elegance when the matter is defining math
      formulas, running functions over elements, and so on. But it seems
      most of that elegance goes away when the problem derails a bit. <br>
      <br>
      Now for my problem I come up with this:<br>
      <blockquote>modify mat x y f =<br>
        &nbsp; let (lrows, row, rrows) = getpart mat x<br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (lcols, col, rcols) = getpart row y<br>
        &nbsp; in lrows ++ [lcols ++ [f col] ++ rcols] ++ rrows<br>
        &nbsp; where<br>
        &nbsp;&nbsp;&nbsp; getpart xs x = let (ls, r:rs) = splitAt x xs in (ls, r, rs)<br>
        <br>
        m0 = [[1,2,3], [4,5,6], [7,8,9]]<br>
        <br>
        main = do<br>
        &nbsp; print m0<br>
        &nbsp; let m1 = modify m0 1 1 succ<br>
        &nbsp; let m2 = modify m1 2 0 pred<br>
        &nbsp; print m2<br>
      </blockquote>
      Which is a bit "awkward" considering the ease it is done in other
      languages.<br>
      <br>
      On 08/14/12 19:35, Tim Perry wrote:<br>
    </div>
    <blockquote
cite="mid:CAFVgASVdA2MBJgT68CgUZuiDbZ-B-GQRPBwdtnSz9UG=QgiOpw@mail.gmail.com"
      type="cite">There is a way. Please try to figure it out and if you
      fail post back with your code and we can help you from there.
      <div><br>
      </div>
      <div><br>
        <br>
        <div class="gmail_quote">On Tue, Aug 14, 2012 at 11:05 AM,
          Carlos J. G. Duarte <span dir="ltr">&lt;<a
              moz-do-not-send="true"
              href="mailto:carlos.j.g.duarte@gmail.com" target="_blank">carlos.j.g.duarte@gmail.com</a>&gt;</span>
          wrote:<br>
          <blockquote class="gmail_quote" style="margin:0 0 0
            .8ex;border-left:1px #ccc solid;padding-left:1ex">Ok, you
            all have been showing examples of running functions over
            elements. Add one, append value, and so on.<br>
            This works well if there's one or more operations to apply
            indistinctly to a number of elements.<br>
            <br>
            Now, what if we just want to make a single operation to a
            single element?<br>
            For example, let's say I have this square matrix<br>
            [[1,2,3],<br>
            &nbsp;[4,5,6],<br>
            &nbsp;[7,8,9]]<br>
            <br>
            how can we increment the value 5 (position 2,2) *and*
            decrement the value 7 (position 3,1)?<br>
            <br>
            This is a made up example of course, I just want to see /
            learn if there's a way to apply a function to a specific
            subset of elements.
            <div class="HOEnZb">
              <div class="h5"><br>
                <br>
                On 08/14/12 00:06, Jack Henahan wrote:<br>
                <blockquote class="gmail_quote" style="margin:0 0 0
                  .8ex;border-left:1px #ccc solid;padding-left:1ex">
                  Equally,<br>
                  <br>
                  &nbsp; &nbsp; &nbsp;let map' = map . map<br>
                  &nbsp; &nbsp; &nbsp;map' (+1) . map (++[3]) $ [[1,2],[3,4]]<br>
                  &nbsp; &nbsp; &nbsp;-- [[2,3,4],[4,5,4]]<br>
                  <br>
                  And you can really keep stacking those up. I think
                  this approach will be cleaner in the long run.<br>
                  <br>
                  For instance, let's start naming our parts.<br>
                  &nbsp; &nbsp; &nbsp; &nbsp; let list = [[1,2],[3,4]]<br>
                  &nbsp; &nbsp; let map' = map . map<br>
                  &nbsp; &nbsp; let addOne = map' (+1)<br>
                  &nbsp; &nbsp; let appendThree = map (++[3])<br>
                  &nbsp; &nbsp; let reverseInner = map reverse<br>
                  <br>
                  So, from here we can do the following:<br>
                  &nbsp; &nbsp; &nbsp; &nbsp; list<br>
                  &nbsp; &nbsp; -- [[1,2],[3,4]]<br>
                  <br>
                  &nbsp; &nbsp; -- the first example<br>
                  &nbsp; &nbsp; addOne list<br>
                  &nbsp; &nbsp; -- [[2,3],[4,5]]<br>
                  &nbsp; &nbsp; &nbsp; &nbsp; -- now the second example<br>
                  &nbsp; &nbsp; addOne . appendThree $ list<br>
                  &nbsp; &nbsp; -- [[2,3,4],[4,5,4]]<br>
                  <br>
                  &nbsp; &nbsp; -- now add one to all members of the list, append
                  three to the list, reverse the inner lists,<br>
                  &nbsp; &nbsp; -- then add one to all members of the new list<br>
                  <br>
                  &nbsp; &nbsp; addOne . reverseInner . appendThree . addOne $
                  list<br>
                  &nbsp; &nbsp; -- [[4,4,3],[4,6,5]]<br>
                  <br>
                  Now how would you construct that as a list
                  comprehension? With the method I've proposed, you need<br>
                  only use map to operate on the nested lists themselves
                  and map' to operate on the elements of those<br>
                  lists.<br>
                  <br>
                  ====<br>
                  Jack Henahan<br>
                  <a moz-do-not-send="true"
                    href="mailto:jhenahan@uvm.edu" target="_blank">jhenahan@uvm.edu</a><br>
                  &nbsp; &nbsp; &nbsp; &nbsp;<br>
                  On Aug 13, 2012, at 6:41 PM, Christopher Howard &lt;<a
                    moz-do-not-send="true"
                    href="mailto:christopher.howard@frigidcode.com"
                    target="_blank">christopher.howard@frigidcode.com</a>&gt;
                  wrote:<br>
                  <br>
                  <blockquote class="gmail_quote" style="margin:0 0 0
                    .8ex;border-left:1px #ccc solid;padding-left:1ex">
                    On 08/12/2012 09:37 PM, Shakthi Kannan wrote:<br>
                    <blockquote class="gmail_quote" style="margin:0 0 0
                      .8ex;border-left:1px #ccc solid;padding-left:1ex">
                      Hi,<br>
                      <br>
                      --- On Mon, Aug 13, 2012 at 10:51 AM, Christopher
                      Howard<br>
                      &lt;<a moz-do-not-send="true"
                        href="mailto:christopher.howard@frigidcode.com"
                        target="_blank">christopher.howard@frigidcode.com</a>&gt;
                      wrote:<br>
                      | Say, for example, I have the list<br>
                      | [[1,2],[3,4]] and want to add 1 to each inner
                      element, resulting in<br>
                      | [[2,3],[4,5]].<br>
                      \--<br>
                      <br>
                      Like this?<br>
                      <br>
                      ghci&gt; let xxs = [[1,2], [3,4]]<br>
                      <br>
                      ghci&gt; [ [ x+1 | x &lt;- xs] | xs &lt;- xxs ]<br>
                      [[2,3],[4,5]]<br>
                      <br>
                      SK<br>
                      <br>
                    </blockquote>
                    Thanks everyone for the responses. I found the list
                    comprehension<br>
                    approach satisfactory, as it allows me to cleanly
                    modify each layer of<br>
                    the nested array as I unwrap it:<br>
                    <br>
                    code:<br>
                    --------<br>
                    b = [[ x+1<br>
                    &nbsp; &nbsp; | x &lt;- xs ++ [3] ]<br>
                    &nbsp; &nbsp; | xs &lt;- [[1,2],[3,4]] ]<br>
                    <br>
                    *Main&gt; b<br>
                    [[2,3,4],[4,5,4]]<br>
                    --------<br>
                    <br>
                    The only downside is that I have to write the layers
                    out in reverse of<br>
                    the way I would normally think of them, but that
                    isn't too big of a<br>
                    challenge.<br>
                    <br>
                    I'm not sure how that would be done with map in a
                    way that would be neat<br>
                    and readable and wouldn't require declaring extra
                    identifiers. I can't<br>
                    give a fair evaluation of the Lens approach because
                    I don't understand<br>
                    enough of the theory yet.<br>
                    <br>
                    -- <br>
                    <a moz-do-not-send="true"
                      href="http://frigidcode.com" target="_blank">frigidcode.com</a><br>
                    <a moz-do-not-send="true" href="http://indicium.us"
                      target="_blank">indicium.us</a><br>
                    <br>
                    _______________________________________________<br>
                    Beginners mailing list<br>
                    <a moz-do-not-send="true"
                      href="mailto:Beginners@haskell.org"
                      target="_blank">Beginners@haskell.org</a><br>
                    <a moz-do-not-send="true"
                      href="http://www.haskell.org/mailman/listinfo/beginners"
                      target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
                  </blockquote>
                  <br>
                  _______________________________________________<br>
                  Beginners mailing list<br>
                  <a moz-do-not-send="true"
                    href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
                  <a moz-do-not-send="true"
                    href="http://www.haskell.org/mailman/listinfo/beginners"
                    target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
                  <br>
                </blockquote>
                <br>
                <br>
                _______________________________________________<br>
                Beginners mailing list<br>
                <a moz-do-not-send="true"
                  href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
                <a moz-do-not-send="true"
                  href="http://www.haskell.org/mailman/listinfo/beginners"
                  target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
              </div>
            </div>
          </blockquote>
        </div>
        <br>
      </div>
    </blockquote>
    <br>
  </body>
</html>