Re: [racket-users] Match: non-greedy, and also repeated elements?
Right... except that I completely misread your first example, which is not at all the same as my example with `cons` patterns. Sorry about that. On Thu, Dec 31, 2020 at 2:06 PM Jon Zeppieri wrote: > > On Wed, Dec 30, 2020 at 2:24 PM David Storrs wrote: > > > > First off, is there a way to make ... in a pattern match non-greedily? > > i.e., match as *few* elements as possible instead of as many? > > As far as I know, no. However, if your first example is really > illustrative of what you're trying to do, you could just use a `cons` > pattern instead of a `list` pattern: > > (match (list '(a b c) '(d e c)) > [(list (cons _ xs) (cons _ ys)) >#:when (equal? xs ys) >'ok] > [else 'nope]) > => 'nope -- 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. To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/CAKfDxxw3Pywy_Hh%3D7H3CRKT2OfnqAksb3r2jwMrqvrNvDqdt7g%40mail.gmail.com.
Re: [racket-users] Match: non-greedy, and also repeated elements?
On Wed, Dec 30, 2020 at 2:24 PM David Storrs wrote: > > First off, is there a way to make ... in a pattern match non-greedily? i.e., > match as *few* elements as possible instead of as many? As far as I know, no. However, if your first example is really illustrative of what you're trying to do, you could just use a `cons` pattern instead of a `list` pattern: (match (list '(a b c) '(d e c)) [(list (cons _ xs) (cons _ ys)) #:when (equal? xs ys) 'ok] [else 'nope]) => 'nope And that makes a nice segue into your second question: > Second, is there a way to make one pattern refer to an earlier pattern in the > same match clause? If we're limiting this to variable patterns, then yes. The documentation says: "If an id is used multiple times within a pattern, the corresponding matches must be the same according to (match-equality-test), except that instances of an id in different or and not sub-patterns are independent." So, assuming that `(match-equality-test)` is `equal?` -- which is the default -- the above could also be written as: (match (list '(a b c) '(d e c)) [(list (cons _ xs) (cons _ xs)) 'ok] [else 'nope]) Similarly, your second example could be written as: (match '(a b c c d) [(list _ ... x x _ ...) 'ok] [else 'nope]) However, I have seen some issues with this feature (using the same var pattern multiple times to get an equality restriction). Most recently: https://github.com/racket/racket/issues/3425 - Jon -- 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. To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/CAKfDxxzm-hq8DRVF5-Ur_Q%3DdxCeQVeARERjKzxQ%2BVUU-18dsrg%40mail.gmail.com.
[racket-users] Match: non-greedy, and also repeated elements?
First off, is there a way to make ... in a pattern match non-greedily? i.e., match as *few* elements as possible instead of as many? Second, is there a way to make one pattern refer to an earlier pattern in the same match clause? Semi-regularly I find myself wanting to do something like 'match repeated elements' or 'match if items from these two lists match'. For example: (match (list '(a b c) '(d e c)) [(list (list _ ... x) (list _ ... y)) #:when (equal? x y) 'ok] [else 'nope]) => 'ok (match '(a b c c d) [(list _ ... x y _ ...) #:when (equal? x y) 'ok] [else 'nope]) => 'ok Is there a way to do this without needing a #:when clause? -- 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. To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/CAE8gKoe78-7sBjHu%3DoR6kg4t4xqaTXjOAa1bHoE_G%2BZKpW6%3DTw%40mail.gmail.com.