I'm trying to write a function that does the following:
> partitions([a,b,c,d])
< [
[[a,b],[c,d]],
[[a,c],[b,d]],
[[a,d],[b,c]]
]
i.e. it finds all partitions of size 2.
Currently I'm trying to do it recursively: at each call generate a list of pairs, and for each pair generated, call the method again on the list with that pair removed. This works, but it generates duplicates. Removing the duplicates requires comparing each element in the partitions generated, this is super slow.
I was wondering if there is a smarter way to do this.