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

From: Dave Herman (dherman at ccs.neu.edu)
Date: Fri Jan 6 11:07:11 EST 2006

> a) prioritize readability:
> (let* ([first (car l)]
>        [middle (reverse (cdr (reverse (cdr l))))]
>        [last (car (reverse l))])
>   `(,(first-proc first) ,@(map middle-proc middle) ,(last-proc last)))
> ... This one seems grotesquely inefficient, even though
>   1) it's not asymptotically any slower, and
>   2) you can get rid of one of the three calls to 'reverse'.

Even more readable, though with the same inefficiencies:

   (require (lib "list.ss" "srfi" "1"))

   (let* ([initial (first ls)]
          [middle (drop-right ls 1)]
          [final (last ls)])
     `(,(p1 initial) ,@(map p2 middle) ,(p3 final)))


Posted on the users mailing list.