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"><<a href="mailto:carlos.j.g.duarte@gmail.com" target="_blank">carlos.j.g.duarte@gmail.com</a>></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>
[4,5,6],<br>
[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>
let map' = map . map<br>
map' (+1) . map (++[3]) $ [[1,2],[3,4]]<br>
-- [[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>
let list = [[1,2],[3,4]]<br>
let map' = map . map<br>
let addOne = map' (+1)<br>
let appendThree = map (++[3])<br>
let reverseInner = map reverse<br>
<br>
So, from here we can do the following:<br>
list<br>
-- [[1,2],[3,4]]<br>
<br>
-- the first example<br>
addOne list<br>
-- [[2,3],[4,5]]<br>
-- now the second example<br>
addOne . appendThree $ list<br>
-- [[2,3,4],[4,5,4]]<br>
<br>
-- now add one to all members of the list, append three to the list, reverse the inner lists,<br>
-- then add one to all members of the new list<br>
<br>
addOne . reverseInner . appendThree . addOne $ list<br>
-- [[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 href="mailto:jhenahan@uvm.edu" target="_blank">jhenahan@uvm.edu</a><br>
<br>
On Aug 13, 2012, at 6:41 PM, Christopher Howard <<a href="mailto:christopher.howard@frigidcode.com" target="_blank">christopher.howard@<u></u>frigidcode.com</a>> 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>
<<a href="mailto:christopher.howard@frigidcode.com" target="_blank">christopher.howard@<u></u>frigidcode.com</a>> 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> let xxs = [[1,2], [3,4]]<br>
<br>
ghci> [ [ x+1 | x <- xs] | xs <- 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>
| x <- xs ++ [3] ]<br>
| xs <- [[1,2],[3,4]] ]<br>
<br>
*Main> 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 href="http://frigidcode.com" target="_blank">frigidcode.com</a><br>
<a href="http://indicium.us" target="_blank">indicium.us</a><br>
<br>
______________________________<u></u>_________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/<u></u>mailman/listinfo/beginners</a><br>
</blockquote>
<br>
______________________________<u></u>_________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/<u></u>mailman/listinfo/beginners</a><br>
<br>
</blockquote>
<br>
<br>
______________________________<u></u>_________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/<u></u>mailman/listinfo/beginners</a><br>
</div></div></blockquote></div><br></div>