[plt-scheme] idiomatic way to split a list into first, middle, last?
On Thu, 2006-01-05 at 20:03 -0500, Dave Herman wrote:
> > (match l
> > [`(,first ,middle ... ,last)
> > `(,(first-proc first) ,@(map middle-proc middle) ,(last-proc last))])
>
> If I'm not mistaken, the PLT syntax-rules and syntax-case languages can
> both handle this kind of thing:
>
> (define-syntax foo
> (syntax-rules ()
> [(_ a b ... c)
> '(a c b ...)]))
>
> > (foo z x1 x2 x3 y)
> (z y x1 x2 x3)
>
> But I believe the match library does not, as you found out. It's not
> hard to generalize a matching algorithm to handle a fixed number of
> suffixes. Maybe the best thing would be for you to improve the match
> library! :)
Just in case anyone took this seriously, while generalizing the
algorithm isn't hard, the code for this is pretty impenetrable. See
collects/mzlib/private/ddk-handlers.ss, among others.
sam th