On 03/20/2013 02:24 AM, Alessandro Campagni wrote:
> Hi everybody,
> this is my first post here, I'm a very beginner so pardon me if my
> question seems trivial.
Hello,
and welcome. Please feel free to ask anything even if it looks trivial.
>
> I'm building a list starting from a list of lists, appending to each
> one every single value taken from another list. Let me get this
> straight with an example:
>
> this is the starting list
> ts:[[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
> now for every [x,y] i should concatenate it with every element of the
> following list [y+1,y+2,...,n] where n is fixed.
>
> This is what I tried:
>
> map (lambda ([x], append (makelist (append (x, [y]), y, last (x) + 1,
> 5))), ts);
>
> map (lambda ([x], map (lambda ([y], append (x, [y])), makelist (z, z,
> last (x) + 1, 5))), ts);
>
> makelist (makelist (append (x, [y]), y, last (x) + 1, 5), x, ts);
>
> I always get:
> [[[1,2,3],[1,2,4],[1,2,5]],[[1,3,4],[1,3,5]],[[1,4,5]],[[2,3,4],[2,3,5]],[[2,4,5]],[[3,4,5]]]
>
> but what I really need is
> [[1,2,3],[1,2,4],[1,2,5],[1,3,4],[1,3,5],[1,4,5],[2,3,4],[2,3,5],[2,4,5],[3,4,5]]
> (i.e. just a list of lists).
>
> Now, the question: Is it possible without using for's or if's?
>
> I played around with flatten but it didn't help me!
>
Well, that's definitely not a trivial question :) I don't know how to
make flatten to flatten just one level of lists, so the only solution I
can think of is using a for loop:
(%i1) ts: [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]$
(%i2) p:[]$
(%i3) for x in ts do p:append (p, makelist (append (x, [y]), y, last (x)
+ 1, 5))$
(%i4) p;
(%o4) [[1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 3, 4], [1, 3, 5], [1, 4, 5],
[2, 3, 4], [2, 3, 5], [2, 4, 5], [3, 4, 5]]
Regards,
Jaime