[Haskell-beginners] cleanest way to unwrap a list?
christopher.howard at frigidcode.com
Tue Aug 14 00:41:27 CEST 2012
On 08/12/2012 09:37 PM, Shakthi Kannan wrote:
> --- On Mon, Aug 13, 2012 at 10:51 AM, Christopher Howard
> <christopher.howard at frigidcode.com> wrote:
> | Say, for example, I have the list
> | [[1,2],[3,4]] and want to add 1 to each inner element, resulting in
> | [[2,3],[4,5]].
> Like this?
> ghci> let xxs = [[1,2], [3,4]]
> ghci> [ [ x+1 | x <- xs] | xs <- xxs ]
Thanks everyone for the responses. I found the list comprehension
approach satisfactory, as it allows me to cleanly modify each layer of
the nested array as I unwrap it:
b = [[ x+1
| x <- xs ++  ]
| xs <- [[1,2],[3,4]] ]
The only downside is that I have to write the layers out in reverse of
the way I would normally think of them, but that isn't too big of a
I'm not sure how that would be done with map in a way that would be neat
and readable and wouldn't require declaring extra identifiers. I can't
give a fair evaluation of the Lens approach because I don't understand
enough of the theory yet.
-------------- next part --------------
A non-text attachment was scrubbed...
Size: 554 bytes
Desc: OpenPGP digital signature
More information about the Beginners