function like Mathematica's Tuples



John Lapeyre escribi?:
> cartesian_product will be equivalent for some problems.
> But it works on sets, so you would have to convert
> from sets to lists, if you want lists. The
> sets will be put in a canonical order and the same
> element cannot occur more than once within a set,
> which may not be what you want (and is different from
> Mma Tuples)
>
>  FWIW
>   

Sets can be converted into lists by means of listify.


(%i1) tuples(lis, n) :=
  block(
    [ len_lis, aux_lis, aux_set, cart_prod],
    len_lis : length(lis),
    aux_lis : makelist(k,k,1,len_lis),
    aux_set : setify(aux_lis),
    cart_prod : apply(cartesian_product, makelist(aux_set,k,1,n)),
    subst(map("=", aux_lis, lis), listify(cart_prod)) ) $
(%i2) tuples([0,1], 2);
(%o2)                  [[0, 0], [0, 1], [1, 0], [1, 1]]
(%i3) tuples([1,0], 3);
(%o3) [[1, 1, 1], [1, 1, 0], [1, 0, 1], [1, 0, 0], [0, 1, 1], [0, 1, 0],
                                                          [0, 0, 1], [0, 
0, 0]]
(%i4) tuples([a,a,b],2);
(%o4) [[a, a], [a, a], [a, b], [a, a], [a, a], [a, b], [b, a], [b, a], 
[b, b]]

--
Mario