The ready made function
chunksOf works very well. When tasked to create 3 elements in sublists with 11 elements in the source list, two elements will be in the last sublist of the result. The following function also includes trailers.
mklsts n = takeWhile (not.null) . map (take n) . iterate (drop n)
I use this as
pairs with a 2 for
n and no
n parameter. Pairs rock.
The match up of
splitOn is one made in hell. In the questioner above, placing
splitOn in a
lambda may have compounded the problems. It is possible to make
splitOn work with
iterate but you have to ditch the
fst of the tuple produced. That defeats the entire purpose. It is way cleaner and easier to use
drop n with
iterate. The results are the same. That's what the preceding function does. Otherwise, it's the same idea.
Here is a novel way of producing the identical results using
tails imported from
Data.List in a list comprehension. It picks up stragglers, too.
ts n ls = [take n l|l<-init$tails ls,odd (head l)]
The parameters are size-of-sublist and list
Well, since I had some time at work a list comprehension version that does not use
tails, a recursive version and a
ttx s ls=[take s.drop x$ls|x<-[0,s..s*1000]]
tkn n =;tkn n xs=[take n xs]++(tkn n $ drop n xs)
tp n ls=takeWhile(not.null)$ map(take n.flip drop ls) [0,n..]
The list comprehension is virtually infinite. Change
[0,s..] for true infinity. The recursive is, of course, inherently infinite and the map function uses a big
takeWhile (not.null) to end itself.