List rooted trees

Bags are represented by Raku type Bag.

use v6;

multi expand-tree ( Bag $tree ) {
    bag(bag(bag()) (+) $tree) (+)
    [(+)] (
        $tree.keys ==> map {
            $^a.&expand-tree.map: * (+) ( $tree (-) bag($^a) )
        }
    );
}

multi expand-trees ( Bag $trees ) {
    [(+)] $trees.keys.map:  { $_.&expand-tree } ;
}      

my $n = 5;
for ( bag(), bag(bag()), *.&expand-trees ... * )[$n] {
    print ++$,".\t";
    .say
};

Output:

The bag bag(bag(bag()), bag()(2)) coresponds with ((())()()). There are two independent ways how we can get it by nesting 4 bags.

Last updated

Was this helpful?