[Haskell-cafe] Nested Lists

Paul Keir pkeir at dcs.gla.ac.uk
Thu Jun 4 08:01:45 EDT 2009

```Hi all,

If I have a list, and I'd like to convert it to a list of lists,
each of length n, I can use a function like bunch:

bunch _ [] = []
bunch n as = let (c,cs) = splitAt n as in c:bunch n cs

> bunch 8 [1..16]
[[1,2,3,4,5,6,7,8],[9,10,11,12,13,14,15,16]]

If I now want to do the same for the nested lists, I can compose
an application involving both map and bunch:

> map (bunch 4) . bunch 8 \$ [1..16]
[[[1,2,3,4],[5,6,7,8]],[[9,10,11,12],[13,14,15,16]]]

and I can "bunch" the new length 4 lists again:

> map (map (bunch 2)) . map (bunch 4) . bunch 8 \$ [1..16]
[[[[1,2],[3,4]],[[5,6],[7,8]]],[[[9,10],[11,12]],[[13,14],[15,16]]]]

Clearly there is a pattern here involving the bunch function and
latterly, three Int parameters; 2, 4 and 8. My question is, can I
create a function that will take such parameters as a list, and
give the same result, for example:

> f [2,4,8] [1..16]
[[[[1,2],[3,4]],[[5,6],[7,8]]],[[[9,10],[11,12]],[[13,14],[15,16]]]]

or perhaps:

> f [bunch 2, bunch 4, bunch 8] [1..16]
[[[[1,2],[3,4]],[[5,6],[7,8]]],[[[9,10],[11,12]],[[13,14],[15,16]]]]

I think it may not be possible because the type signature of f would
depend on the length of its list parameter; but I'm not sure.

-Paul
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090604/39bb4ea9/attachment.html
```

More information about the Haskell-Cafe mailing list