Re: [racket-users] Destructuring a list in (for ...)
Depends on what `x` is, both symbolically and in terms of binding: (for/first ([(list _) #hash([(a) . 1])]) list) (define-match-expander x (syntax-rules () [(_ pat) (list pat)])) (match '(ok) [(x v) v]) -Philip On Fri, Nov 23, 2018 at 11:41 PM Greg Hendershott wrote: > > The trouble, sadly, is that this grammar is ambiguous. In > > > > (for ([(x y) s]) > > ) > > > > should (x y) be parsed as a single match pattern or as two binders for a > two-valued sequence (such as one produced by in-hash, for example)? > > Unless I'm being dense, (x y) isn't a valid single match pattern? > > [Quasipatterns like `(x y) or `(,x ,y) would be.] > > But even if it is grammatically possible, like I said at the end, I'm > not sure it's worthwhile. > > -- > 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.
Re: [racket-users] Destructuring a list in (for ...)
> The trouble, sadly, is that this grammar is ambiguous. In > > (for ([(x y) s]) > ) > > should (x y) be parsed as a single match pattern or as two binders for a > two-valued sequence (such as one produced by in-hash, for example)? Unless I'm being dense, (x y) isn't a valid single match pattern? [Quasipatterns like `(x y) or `(,x ,y) would be.] But even if it is grammatically possible, like I said at the end, I'm not sure it's worthwhile. -- 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.
Re: [racket-users] Destructuring a list in (for ...)
I actually have an experimental `in-match` macro, analogous to `in-value`: https://docs.racket-lang.org/adjutor/Experimental.html#(form._((lib._adjutor%2Fmain..rkt)._in-match)) One open question with my version (and one of the reasons I consider this experimental) is what should be done if one of the identifiers isn't bound by the match pattern (or is bound by some but not all of the patterns) but shadows another binding. Right now the binding from context is used, which is nice in some cases but would probably be confusing in others. It might be better to make that a syntax error. -Philip On Fri, Nov 23, 2018 at 1:29 PM Laurent wrote: > Maybe you could have something like (in-match ...) ? > > On Fri, Nov 23, 2018 at 4:54 PM Alexis King wrote: > >> The trouble, sadly, is that this grammar is ambiguous. In >> >> (for ([(x y) s]) >> ) >> >> should (x y) be parsed as a single match pattern or as two binders for a >> two-valued sequence (such as one produced by in-hash, for example)? You >> could make it unambiguous in various ways, such as by requiring uses of >> match patterns to all use the multi-valued binder syntax (with an extra set >> of parens), but that’s a bit clumsy. Unfortunately, I can’t think of any >> particularly elegant way to support match patterns in for loop binders in a >> backwards-compatible way. >> >> Alexis >> >> > On Nov 22, 2018, at 20:14, Greg Hendershott >> wrote: >> > >> >> (define-syntax (match-for stx) >> > >> > That's nice. >> > >> > Sometimes I wish I could do the general thing -- use `match` patterns >> > in the binding clauses for any `for`-family form. >> > >> > I often do something like this: >> > >> >(define xs (list (cons 1 2) (cons 3 4))) >> > >> >(for ([x (in-list xs)]) >> > (match-define (cons a b) x) >> > (use a b)) >> > >> > Instead it would be nice to write: >> > >> >(for ([(match-define (cons a b)) (in-list xs)]) >> > (use a b)) >> > >> > Or even just: >> > >> >(for ([(cons a b c) (in-list xs)]) >> > (use a b)) >> > >> > In the grammar, `id` becomes `id-or-match-pattern`. >> > >> > On the other hand, this would only really help in simple >> > `match-define` destructuring -- as opposed to using `match` to handle >> > variations in the data. And although I do the former a lot, I do the >> > latter even more. >> >> -- >> 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. > -- 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.
Re: [racket-users] Destructuring a list in (for ...)
Maybe you could have something like (in-match ...) ? On Fri, Nov 23, 2018 at 4:54 PM Alexis King wrote: > The trouble, sadly, is that this grammar is ambiguous. In > > (for ([(x y) s]) > ) > > should (x y) be parsed as a single match pattern or as two binders for a > two-valued sequence (such as one produced by in-hash, for example)? You > could make it unambiguous in various ways, such as by requiring uses of > match patterns to all use the multi-valued binder syntax (with an extra set > of parens), but that’s a bit clumsy. Unfortunately, I can’t think of any > particularly elegant way to support match patterns in for loop binders in a > backwards-compatible way. > > Alexis > > > On Nov 22, 2018, at 20:14, Greg Hendershott > wrote: > > > >> (define-syntax (match-for stx) > > > > That's nice. > > > > Sometimes I wish I could do the general thing -- use `match` patterns > > in the binding clauses for any `for`-family form. > > > > I often do something like this: > > > >(define xs (list (cons 1 2) (cons 3 4))) > > > >(for ([x (in-list xs)]) > > (match-define (cons a b) x) > > (use a b)) > > > > Instead it would be nice to write: > > > >(for ([(match-define (cons a b)) (in-list xs)]) > > (use a b)) > > > > Or even just: > > > >(for ([(cons a b c) (in-list xs)]) > > (use a b)) > > > > In the grammar, `id` becomes `id-or-match-pattern`. > > > > On the other hand, this would only really help in simple > > `match-define` destructuring -- as opposed to using `match` to handle > > variations in the data. And although I do the former a lot, I do the > > latter even more. > > -- > 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.
Re: [racket-users] Destructuring a list in (for ...)
The trouble, sadly, is that this grammar is ambiguous. In (for ([(x y) s]) ) should (x y) be parsed as a single match pattern or as two binders for a two-valued sequence (such as one produced by in-hash, for example)? You could make it unambiguous in various ways, such as by requiring uses of match patterns to all use the multi-valued binder syntax (with an extra set of parens), but that’s a bit clumsy. Unfortunately, I can’t think of any particularly elegant way to support match patterns in for loop binders in a backwards-compatible way. Alexis > On Nov 22, 2018, at 20:14, Greg Hendershott wrote: > >> (define-syntax (match-for stx) > > That's nice. > > Sometimes I wish I could do the general thing -- use `match` patterns > in the binding clauses for any `for`-family form. > > I often do something like this: > >(define xs (list (cons 1 2) (cons 3 4))) > >(for ([x (in-list xs)]) > (match-define (cons a b) x) > (use a b)) > > Instead it would be nice to write: > >(for ([(match-define (cons a b)) (in-list xs)]) > (use a b)) > > Or even just: > >(for ([(cons a b c) (in-list xs)]) > (use a b)) > > In the grammar, `id` becomes `id-or-match-pattern`. > > On the other hand, this would only really help in simple > `match-define` destructuring -- as opposed to using `match` to handle > variations in the data. And although I do the former a lot, I do the > latter even more. -- 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.
Re: [racket-users] Destructuring a list in (for ...)
> (define-syntax (match-for stx) That's nice. Sometimes I wish I could do the general thing -- use `match` patterns in the binding clauses for any `for`-family form. I often do something like this: (define xs (list (cons 1 2) (cons 3 4))) (for ([x (in-list xs)]) (match-define (cons a b) x) (use a b)) Instead it would be nice to write: (for ([(match-define (cons a b)) (in-list xs)]) (use a b)) Or even just: (for ([(cons a b c) (in-list xs)]) (use a b)) In the grammar, `id` becomes `id-or-match-pattern`. On the other hand, this would only really help in simple `match-define` destructuring -- as opposed to using `match` to handle variations in the data. And although I do the former a lot, I do the latter even more. -- 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.
Re: [racket-users] Destructuring a list in (for ...)
I like that. I really need to level up on Racket macros! On Wednesday, November 21, 2018 at 3:19:55 PM UTC-5, Matthew Butterick wrote: > > > On Nov 21, 2018, at 9:30 AM, Brian Adkins > wrote: > > Thanks guys. I think I can live with: > > (for ([(i j) (in-dict '(("a" . 1) ("b" . 20)))]) > (display (list i j))) > > I suppose I could also write something analogous to in-dict that would do > what I want. Maybe in-tuple ? > > > Or perhaps: > > #lang racket > (require (for-syntax racket/syntax)) > > (define-syntax (match-for stx) > (syntax-case stx () > [(_ ([PAT SEQ] ...) . BODY) > (with-syntax ([(TEMP ...) (map (λ (stx) (generate-temporary)) > (syntax->list #'(PAT ...)))]) >#'(for ([TEMP SEQ] ...) > (match-let ([PAT TEMP] ...) > . BODY)))])) > > (match-for ([(list i j) '(("a" 1) ("b" 20))]) >(display (list i j))) > -- 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.
Re: [racket-users] Destructuring a list in (for ...)
> On Nov 21, 2018, at 9:30 AM, Brian Adkins wrote: > > Thanks guys. I think I can live with: > > (for ([(i j) (in-dict '(("a" . 1) ("b" . 20)))]) > (display (list i j))) > > I suppose I could also write something analogous to in-dict that would do > what I want. Maybe in-tuple ? Or perhaps: #lang racket (require (for-syntax racket/syntax)) (define-syntax (match-for stx) (syntax-case stx () [(_ ([PAT SEQ] ...) . BODY) (with-syntax ([(TEMP ...) (map (λ (stx) (generate-temporary)) (syntax->list #'(PAT ...)))]) #'(for ([TEMP SEQ] ...) (match-let ([PAT TEMP] ...) . BODY)))])) (match-for ([(list i j) '(("a" 1) ("b" 20))]) (display (list i j))) -- 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.
Re: [racket-users] Destructuring a list in (for ...)
Thanks guys. I think I can live with: (for ([(i j) (in-dict '(("a" . 1) ("b" . 20)))]) (display (list i j))) I suppose I could also write something analogous to in-dict that would do what I want. Maybe in-tuple ? On Wednesday, November 21, 2018 at 11:58:49 AM UTC-5, Vincent St-Amour wrote: > > `in-dict` can get you mostly there. > > (for ([(i j) (in-dict '(("a" 1) ("b" 20)))]) > (display (list i j))) > > > (a (1))(b (20)) > > If you have lists of pairs instead of lists of lists, you'll get the > same result as the hash case. > > Vincent > > > On Wed, 21 Nov 2018 10:55:23 -0600, > Brian Adkins wrote: > > > > I thought it was possible to destructure a list in for, but I've been > searching/experimenting for a while without success. I noticed this example > in the docs: > > > > (for ([(i j) #hash(("a" . 1) ("b" . 20))]) > > (display (list i j))) > > > > So, I assumed I could do this: > > > > (for ([(i j) '(("a" 1) ("b" 20))]) > > (display (list i j))) > > > > But that doesn't work. I'm trying to avoid something as verbose as: > > > > (for ([(pair) '(("a" 1) ("b" 20))]) > > (match-let ([(list i j) pair]) > > (display (list i j > > > > Why do elements of a Hash provide 2 values to for, where a 2-tuple list > does not? Is there a more direct way to destructure a list in for? > > > > Thanks, > > Brian > > > > -- > > 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...@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.
Re: [racket-users] Destructuring a list in (for ...)
`in-dict` can get you mostly there. (for ([(i j) (in-dict '(("a" 1) ("b" 20)))]) (display (list i j))) > (a (1))(b (20)) If you have lists of pairs instead of lists of lists, you'll get the same result as the hash case. Vincent On Wed, 21 Nov 2018 10:55:23 -0600, Brian Adkins wrote: > > I thought it was possible to destructure a list in for, but I've been > searching/experimenting for a while without success. I noticed this example > in the docs: > > (for ([(i j) #hash(("a" . 1) ("b" . 20))]) > (display (list i j))) > > So, I assumed I could do this: > > (for ([(i j) '(("a" 1) ("b" 20))]) > (display (list i j))) > > But that doesn't work. I'm trying to avoid something as verbose as: > > (for ([(pair) '(("a" 1) ("b" 20))]) > (match-let ([(list i j) pair]) > (display (list i j > > Why do elements of a Hash provide 2 values to for, where a 2-tuple list does > not? Is there a more direct way to destructure a list in for? > > Thanks, > Brian > > -- > 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.
Re: [racket-users] Destructuring a list in (for ...)
You can use `in-dict` on an alist, and it will behave like the default sequence behavior for hashes. Note that it assumes the elements are cons pairs, not lists, so the values in your example will be '(1) and '(20). Sam On Wed, Nov 21, 2018 at 11:55 AM Brian Adkins wrote: > > I thought it was possible to destructure a list in for, but I've been > searching/experimenting for a while without success. I noticed this example > in the docs: > > (for ([(i j) #hash(("a" . 1) ("b" . 20))]) > (display (list i j))) > > So, I assumed I could do this: > > (for ([(i j) '(("a" 1) ("b" 20))]) > (display (list i j))) > > But that doesn't work. I'm trying to avoid something as verbose as: > > (for ([(pair) '(("a" 1) ("b" 20))]) > (match-let ([(list i j) pair]) >(display (list i j > > Why do elements of a Hash provide 2 values to for, where a 2-tuple list does > not? Is there a more direct way to destructure a list in for? > > Thanks, > Brian > > -- > 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.
[racket-users] Destructuring a list in (for ...)
I thought it was possible to destructure a list in for, but I've been searching/experimenting for a while without success. I noticed this example in the docs: (for ([(i j) #hash(("a" . 1) ("b" . 20))]) (display (list i j))) So, I assumed I could do this: (for ([(i j) '(("a" 1) ("b" 20))]) (display (list i j))) But that doesn't work. I'm trying to avoid something as verbose as: (for ([(pair) '(("a" 1) ("b" 20))]) (match-let ([(list i j) pair]) (display (list i j Why do elements of a Hash provide 2 values to for, where a 2-tuple list does not? Is there a more direct way to destructure a list in for? Thanks, Brian -- 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.