[EMAIL PROTECTED] wrote:

I'll show you.  Here are some of the generators.  This is very dense,
functional code.  Read at your own risk (but I'm certainly not writing
it to be executed!).

Quite. ;)

For the regexp /a aa aaa aaaa aaaaa aaaaaa/, this would sequentially search through all possible ways to decompose 21 until it hit 1+2+3+4+5+6; then it'd return "a"x21 as the single result. This... might take a while.

Since programming in Theoretic Perl6 (Terl6?) /is/ fun, here's my take:

# compositions2($length, @listoflists) gives all possible ways to depompose
# $length into the sum of the form a1+a2+a3+...+an so that a[i] is contained
# in @listoflists[i]


multi generate(Rule::Group $group: Int $length) {
# For each assignent of lengths to each of $groups children
# such that they sum to $length...
compositions2($length, map(&possible_lengths, $group.children)) ==> map -> @comp {
@comp  $group.children ==> map -> $n, $pat {
# Generate every string of length $n that the subpattern
# matches
[ $pat.generate($n) ]
} ==>


       # Join our results together
       outer ==> join ''
   }
}

use IntegerIntervalArithmetics << add_lists, mult_lists >>;

# add_lists(@list1, @list2) returns the list of sums of all possible
# elements of the lists. However, it should take .. and ... operators
# into account. This generally requires range tree datastructure of some
# sort.
# mult_lists(@list1, @list2) returns the list of all products

multi possible_lengths(Rule::Group $group) {
   [ reduce(&add_lists, map(&possible_lengths, $group.children)) ];
}

multi possible_lengths(Rule::Plus $plus) {
[ mult_lists($plus.expression, 1... ];
}


multi possible_lengths(Rule::Constant $const) {
[ length($const.chars) ];
}


I'm still not claiming that this is something one should use, but it /was/ fun
to tinker with. :)

   Miro



Reply via email to