axiom-developer
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Axiom-developer] How to enumerate a product of lists (or integer se


From: Martin Rubey
Subject: Re: [Axiom-developer] How to enumerate a product of lists (or integer sets)
Date: 15 Oct 2007 15:20:16 +0200
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.4

Francois Maltey <address@hidden> writes:

> With axiom I only find this multi-line command :
> L := reduce
>  (append, 
>   [reduce
>    (append, 
>     [reduce
>      (append, 
>       [[matrix [[a,b,15-a-b],[c,d,15-c-d],[15-a-c,15-b-d,15-a-d]] 
>         for a in 1..9] for b in 1..9]) for c in 1..9]) for d in 1..9]) ;

you can shorten this in two different ways:

* reduce(append, l) is the same as concat l, thus, you can write

L := concat [concat [concat [[matrix [[a,b,15-a-b],_
                                      [c,d,15-c-d],_
                                      [15-a-c,15-b-d,15-a-d]] _
                              for a in 1..9]_
                             for b in 1..9]_
                     for c in 1..9] _
             for d in 1..9]

* you could first generate all Cartesian products with

L := concat [concat [concat [[[a,b,c,d] _
                              for a in 1..9] _
                             for b in 1..9] _
                     for c in 1..9] _
             for d in 1..9]

and then loop over them:

[eval(matrix [[a,b,15-a-b],[c,d,15-c-d],[15-a-c,15-b-d,15-a-d]], [a,b,c,d], l) 
for l in L]

> a train of for creates only one zip-list.

Yes, and that's something very useful! For example

[f(l) for l in L for i in 1..4]

only uses the first for elements in L, or

[f(l, i) for l in L for i in 1..]

is in my opinion much better to read then

[f(L.i, i) for i in 1..#L]


Martin





reply via email to

[Prev in Thread] Current Thread [Next in Thread]