[plt-scheme] match question
thanks for your response. Would you say that the greediness of
the patterns produces counter-intuitive answers?
Here's a very simple example:
(match '(3 2 4 3 2 1)
((list x y ... x z ...) (append y z)))
=> (2 4 3 2)
Surely the result should be (2 4 2 1)? At least that's the intuitively
correct answer (and the one returned by Mathematica, e.g.).
Changing ... to ..1 produces a different counter-intuitive answer: (2 4 3 1).
For people like me, who are merely using your language rather than trying
to understand its inner workings, this is worrisome.
regards,
praimon
On Sun, May 11, 2008 at 10:37 PM, Sam TH <samth at ccs.neu.edu> wrote:
> The general answer is the all ... patterns are greedy, but in order,
> from left to right.
>
> So we match as many elements to `x' as possible, then one to `y', then
> as many to `z' as possible, then another to the second `y'.
>
> In the particular case, we match as many elements to `x' as possible,
> while allowing the whole pattern to match. That's the first 3
> elements, and the fifth element gets matched to `z'. In your second
> example, we have to match at least two elements to `z', so we only
> match 2 to `x'.
>
> Does that help explain things?
>
> sam th
>
>
>
> On Sat, May 10, 2008 at 6:52 PM, praimon <praimon at gmail.com> wrote:
> > hello,
> > in 3.99.0.24, using scheme/match:
> >
> > (map (lambda (x)
> > (match x ((list x ... y z ..1 y) (append x z))))
> > '((1 2 3 4 3)
> > (1 2 3 4 3 4)
> > (1 2 3 4 3 3)))
> >
> > => ((1 2 4) (1 2 3 3) (1 2 3 3))
> >
> > I understand the first two matches, but
> > shouldn't the last result be (1 2 4 3)?
> >
> > This produces my expected answer:
> >
> > (match '(1 2 3 4 3 3)
> > ((list x ... y z ..2 y) (append x z)))
> >
> > => (1 2 4 3)
> >
> > but in this context, aren't ..1 and ..2 equivalent?
> >
> > thanks,
> > praimon
> > _________________________________________________
> > For list-related administrative tasks:
> > http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> >
>
>
>
> --
> sam th
> samth at ccs.neu.edu
>