You can also try this:
bunch n f = takeWhile (not . null) . unfoldr (Just . (f *** id) . splitAt n)
> (bunch 8 . bunch 4 . bunch 2) id [1..16]
Any way, it's not possible to take list [8, 4, 2], because it's length need
to be known at compile time.
_
On Thu, 4 Jun 2009, Paul Keir wrote:
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
http://hackage.haskell.org/packages/archive/utility-ht/0.0
How about...
*Main> bunch () [1..16]
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
*Main> bunch (8 :+: ()) [1..16]
[[1,2,3,4,5,6,7,8],[9,10,11,12,13,14,15,16]]
*Main> bunch (8 :+: 4 :+: ()) [1..16]
[[[1,2,3,4],[5,6,7,8]],[[9,10,11,12],[13,14,15,16]]]
*Main> bunch (8 :+: 4 :+: 2 :+: ()) [1..16]
1,2]
Hi Paul,
I don't have time to solve your actual problem, but I think it's doable
using associated type families. I attach a module which I'm using in my
current project that does things quite similar to what you're asking for.
For example:
*Main> replicateArray (3 :> IntArr) 4
[4,4,4]
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