What is a good way to represent the type LoL a, being a list of lists
of … of a? The nesting level is arbitrary, but uniform over all
elements of the outer list.
The case I have in mind is to apply a grouping on the members of a
list, and then to apply a next grouping on each subgroup, and so on. It
is not known up front how many groupings one will have to apply. Hence:
rGroupBy :: [(a -> a -> Bool)] -> [a] -> [...[a]...]
Extra brownie points for the type signature of rGroupBy 😉
Example:
Suppose deweyGroup i groups the elements based on the i-th number
rGroupBy [deweyGroup 1, deweyGroup 2]
["1.1", "1.2.1", "1.2.2", "2.1", "2.2", "3"]
gives:
[ [ [ "1.1" ], [ "1.2.1", "1.2.2" ] ],
[ [ "2.1" ], [ "2.2" ] ],
[ [ "3" ] ]
]
Postscript
One day later, we have 4 excellent and complementary solutions. I’m very pleased with the answers; thank you all.
I believe the following example should be close to what you had in mind. First we declare type-level natural numbers. Then we define vectors, which carry their length as a phantom type (see Fixed-length vectors in Haskell, Part 1: Using GADTs). And then we define a structure for nested lists of lists of … which carries the depth as a phantom type. Finally we can define correctly typed
rGroupBy.