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.

restart;

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:

doit(a,b,1);

[[a], [b]]

doit(p,q,3);

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

doit(5,7,3);

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

Download mw.mw

Please Wait...