Given a list of pairs, I'm trying to iterate through the list and bind the first and second elements of each pair to x and y, respectively, in each iteration.
For example, hash sets are a multiple-valued sequence[1], so this: (for/list ([(x y) #hash((1 . 2) (3 . 4))]) x) produces: (list 1 3) i.e. here x gets bound to 1 in the first iteration and 3 in the second. I would like something analogous for a list of pairs, i.e. replacing #hash with ' : (for/list ([(x y) '((1 . 2) (3 . 4))]) x) except that lists are a by default a single-valued sequence[2]. [1] http://docs.racket-lang.org/guide/for.html?q=multiple-valued%20sequence#%28part._.Multiple-.Valued_.Sequences%29 [2] third paragraph in http://docs.racket-lang.org/reference/pairs.html On Mon, Jul 10, 2017 at 9:32 PM, Jon Zeppieri <zeppi...@gmail.com> wrote: > On Mon, Jul 10, 2017 at 11:02 PM, Nadeem Abdul Hamid <nad...@acm.org> > wrote: > > How come this: > > (for/list ([(x y) (in-parallel '(1 2) '(3 4))]) x) > > produces > > '(1 2) > > instead of > > '(1 3) > > ? > > You're creating a list of the x values. In the first iteration x is 1, > and in the second it's 2. So the result is '(1 2). You won't see 3 in > the result, because it will be bound to y, not x, in the first > iteration. > > > Whereas, > > (sequence-ref (in-parallel '(1 2) '(3 4)) 0) > > produces > > 1 > > 3 > > as I would expect, but am I doing something wrong with for/list? > > > Here, you're getting the first element of the sequence which is (values 1 > 3). > > > > > In general, how might one convert a list of tuples into a multiple-valued > > sequence? i.e. ideally I'd like to be able to just match: > > (for/list ([(x y) '((1 2) (3 4))]) x) > > and have it produce > > '(1 3) > > I'm not sure exactly what you're looking for, since I can't quite > square your description ("multiple-values sequence") with your example > result -- '(1 3). If that is the result you're looking for, you could > use > > (map first '((1 2) (3 4))) > > If you want to go from '((1 2) (3 4)) to '((1 3) (2 4)), you could do > > (apply map list '((1 2) (3 4))) > > And if you really do want a sequence of multiple values, you could use > > (apply in-parallel '((1 2) (3 4))) > > -- > You received this message because you are subscribed to the Google Groups > "Racket Users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to racket-users+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.