[Haskell-beginners] cleanest way to unwrap a list?
Christopher Howard
christopher.howard at frigidcode.com
Tue Aug 14 00:41:27 CEST 2012
On 08/12/2012 09:37 PM, Shakthi Kannan wrote:
> Hi,
>
> --- 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 ]
> [[2,3],[4,5]]
>
> SK
>
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:
code:
--------
b = [[ x+1
| x <- xs ++ [3] ]
| xs <- [[1,2],[3,4]] ]
*Main> b
[[2,3,4],[4,5,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
challenge.
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.
--
frigidcode.com
indicium.us
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 554 bytes
Desc: OpenPGP digital signature
URL: <http://www.haskell.org/pipermail/beginners/attachments/20120813/b64eba8a/attachment.pgp>
More information about the Beginners
mailing list