[plt-scheme] idiomatic way to split a list into first, middle, last?

From: Sam Tobin-Hochstadt (samth at ccs.neu.edu)
Date: Thu Jan 5 21:10:42 EST 2006

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



Posted on the users mailing list.