Question: How to generate these combinations?

I expect that there must exist a Maple proc that does the equivalent of the following but I couldn't find it.  Can it be in the combinat package?

And if there isn't one, can the following be improved?  It seems to be horribly inefficient to me, although efficiency is not a major concern for me right now since I need it only for small values of n.


Proc produces all lists of length n consisting of the

two distinct symbols a and b.

doit := proc(a, b, n::posint)
        local p := 1, L := [ [a], [b] ];
        for p from 1 to n-1 do
                 L := [ map( x -> [a,op(x)], L)[], map( x -> [b,op(x)], L)[] ];
        end do:
        return L;
end proc:


[[a], [b]]


[[p, p, p], [p, p, q], [p, q, p], [p, q, q], [q, p, p], [q, p, q], [q, q, p], [q, q, q]]


[[5, 5, 5], [5, 5, 7], [5, 7, 5], [5, 7, 7], [7, 5, 5], [7, 5, 7], [7, 7, 5], [7, 7, 7]]


Please Wait...