On Wed, Aug 15, 2018 at 10:08 PM, Alex Gian <alex9...@gmail.com> wrote:
> Normally in match... > > > (match '(a b b b b b b a) > [`(,x ,y ... ,x) (list 'x: x 'y: y)]) > '(x: a y: (b b b b b b)) > > > (match '(a b b b b b b d) > [`(,x ,y ... ,x) (list 'x: x 'y: y)] > [_ 'nope]) > 'nope > > > So far, so good. > However, in this case: > > > (match '(a b b b b b b c) > [`(a ,x ... ,y ... ,x ... c) (list 'x: x 'y: y)]) > '(x: (b b b b b b) y: ()) > > > (match '(a b b b b b b c) > [`(a ,x ... ,y ..1 ,x ... c) (list 'x: x 'y: y)]) > '(x: (b b b b b) y: (b)) > > '...' is "zero or more" and it looks like it's being greedy. The pattern above says to match: a: one item x ...: as many things as you can (at least zero) y ...: as many things as you can (at least one) x ...: as many things as you can (at least zero) c: one item Using greedy matching your first pattern does this: a gets 'a x gets '(b b b b b b c) Match fails! Nothing to put in y, which must have at least 1 item. Backtrack. x gets (b b b b b b) y gets c x gets zero items, which is fine Match fails! Nothing to put in c, which must have exactly one item. Backtrack. x gets (b b b b b) y gets (b) c gets c Match succeeds! Complete. > > > > I am confused. Shouldn't the two instances of 'x ...' be the same? > In which case, in the first example, 'x' would be '(b b b) and 'y' would > be '(), > In the second 'x' would be '(b b) and 'y' would also be '(b b), no? > That's the way I understand it (and the way it works with most patterns > matchers I've seen). > > What have I missed? > > > > > > > -- > 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.