> On Jul 11, 2017, at 12:38 AM, Nadeem Abdul Hamid <nad...@acm.org> wrote:
> 
> ... though a syntactic solution combining for and match would probably
> be better.
> 
> Yes, please!...
> Look, python can do it:
> 
> >>> [(x, z) for (x, y, z) in [(1, 2, 3), (4, 5, 6), (7, 8, 9), ('a', 'b', 
> >>> 'c')]] 
> [(1, 3), (4, 6), (7, 9), ('a', 'c')]
> 
> :-)


You can do this with the Generic Bind package [1]. It's goal is to allow you to 
use patterns like this in any binding form that accepts generic binding 
instances, including an extended version of for/list.

raco pkg install generic-bind

#lang racket
(require generic-bind)
(~for/list ([($list x y z) '((1 2 3) (4 5 6) (7 8 9) (a b c))])
  (list x z))
;'((1 3) (4 6) (7 9) (a c))

If you want to use the name `for/list` without the `~`, you can require 
`generic-bind/as-rkt-names` instead.

#lang racket
(require generic-bind/as-rkt-names)

(for/list ([($list x y z) '((1 2 3) (4 5 6) (7 8 9) (a b c))])
  (list x z))
;'((1 3) (4 6) (7 9) (a c))

It also allows arbitrary match patterns using the `$` form, so `($ (list x y 
z))` would work the same way.

[1]: https://docs.racket-lang.org/generic-bind/index.html

-- 
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.

Reply via email to