Re: Partitioning a list into unequal partitions
- To: mathgroup at smc.vnet.net
- Subject: [mg47477] Re: Partitioning a list into unequal partitions
- From: Jon Harrop <jdh30 at cam.ac.uk>
- Date: Wed, 14 Apr 2004 07:16:26 -0400 (EDT)
- Organization: University of Cambridge
- References: <c5gfdp$an0$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
DIAMOND Mark R. wrote:
> Could someone please show me a simple (non-procedural) way of partitioning
> a list into 1,2,3 ... n disjoint sublists, where the length of the list is
> guaranteed to be correct (i.e. n*(n+1)/2)
>
> I can't see a way, and yet I'm sure there *must* be a one-liner using
> Fold.
I get:
GrowingSplit[l_] :=
Block[{t = Take[#2, #1]}, If[Length[#2] == #1, {t},
Prepend[#0[1 + #1, Drop[#2, #1]], t]]]&[1, 1]
which gives:
In := GrowingSplit[{1, 2, 2, 3, 3, 3}]
Out := {{1}, {2, 2}, {3, 3, 3}}
I can't think of any way to use Fold though...
This has brought up a couple of questions for me though.
Firstly, I'd rather have used "Function[...]" rather than "(...)&" but how
do you refer to the function itself in the former case?
Secondly, is there a built-in function for splitting a list in two at the
given index? This would be more efficient than calling "Take[]" and then
"Drop[]" with the same arguments.
Cheers,
Jon.