## series-users — Users' discussion list

You can subscribe to this list here.

 2002 2005 2008 2010 2013 Jan Feb (1) Mar Apr May Jun Jul Aug Sep Oct Nov Dec Jan (4) Feb Mar Apr May Jun Jul Aug Sep Oct Nov (1) Dec (19) Jan Feb Mar Apr May Jun (1) Jul Aug (7) Sep Oct Nov Dec Jan Feb Mar Apr May Jun Jul (15) Aug Sep Oct Nov Dec Jan (2) Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
S M T W T F S

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23
(2)
24

25
(2)
26
(4)
27
(3)
28
(4)
29

30

31

Showing 2 results of 2

 Re: [Series-users] Nested loops From: Helmut Eller - 2010-07-23 16:26:11 ```* Raymond Toy [2010-07-23 15:00+0200] writes: > On 7/23/10 4:15 AM, Helmut Eller wrote: >> Hello, >> >> I'd like to create a macro/function that when given two series >> as input produces the "cross product" as output. E.g. >> >> (cross (scan '(a b c)) (scan '(1 2 3))) >> => #Z(a a a b b b c c c) and #Z(1 2 3 1 2 3 1 2 3) >> > Do the two series have the same length? Yes, the output series have the same length (possibly infinite). > It seems you just replicate > each element 3 times. Why 3? Because there are 3 elements in the > original series? Perhaps a bad example, but yes: 3 because the inputs have length 3. > I'm a bit confused on exactly what cross should do. Another way to write CROSS would be: (defmacro cross (sx sy) `(let ((xs '()) (ys '())) (iterate ((x ,sx)) (iterate ((y ,sy)) (push x xs) (push y ys))) (values (scan (reverse xs)) (scan (reverse ys))))) Obviously inefficient, but hopefully clearer. One series expression drives the outer loop and the other series expression drives the inner loop. The outer loop produces a series; lets call that #z(x_0 x_1 ... x_n). The inner loop consumes that series. For each element x_i the inner loop produces (part of) a series #z(y_i_0 y_i_1 ... y_i_m) and additionally copies x_i m times to a second series (so that clients of CROSS can access the corresponding x and y values). The final two outputs look like: #z(x_0 x_0 ... x_0 x_1 x_1 ... x_1 x_2 x_2 ...) #z(y_0_0 y_0_1 ... y_0_m y_1_0 y_1_1 ... y_1_k y_2_0 y_2_1 ...) y_0_0 and y_1_0 can be different; also m and k can be different. The inner loop doesn't necessarily produce the same values for each iteration of the outer loop; i.e. the y values aren't just replicated. [The series expression of the inner loop should also have access to the current x value. In the example that's possible by accessing the variable x but that's sloppy; the name should better be an argument to CROSS or the second expression should be a lambda taking the value of x as argument.] Helmut ```
 [Series-users] Nested loops From: Helmut Eller - 2010-07-23 08:53:29 ```Hello, I'd like to create a macro/function that when given two series as input produces the "cross product" as output. E.g. (cross (scan '(a b c)) (scan '(1 2 3))) => #Z(a a a b b b c c c) and #Z(1 2 3 1 2 3 1 2 3) Or as picture: ----------------- ------- | (scan '(a b c)) |------>| |----> #z(a b c a b c a b c) ----------------- | | | cross | ----------------- | | | (scan '(1 2 3)) |------>| |----> #z(1 1 1 2 2 2 3 3 3) ----------------- ------- I tried this: (defmacro cross (sx sy) `(producing (ox oy) ((genx ,sx) x geny y) (loop (tagbody (setq x (next-in genx (terminate-producing))) (setq geny (generator ,sy)) loop (setq y (progn (next-in geny (go end)))) (next-out ox x) (next-out oy y) (go loop) end)))) (defun foo () (collect (mapping (((x y) (cross (scan '(1 2 3)) (scan '(a b c))))) (cons x y)))) It works but apparently this can't be optimized. Is there a better way to write such "nested loops" where the outputs are "faster" than the inputs? If not, would it be possible to add a new "primitive" for such cases? Helmut ```

Showing 2 results of 2