Zach Beane wrote:
> "bradb" <
brad.beveri...@gmail.com> writes:
> > I'm using CL-PPCRE, and some functions return lists of start and end
> > matches, so a return might be
> > (0 2 4 7 10 15), three matches.
> > Right now I loop over the matches like
> > (loop :for match :on (all-matches scanner text :start start) :by
> > #'cddr
> > :do (let ((start (first match))
> > (end (second match)))
> > (subseq text start end))
>
> > Is there a nicer way to gather the pairs than that?
>
> LOOP destructures. Try this:
>
> (loop for (start end) on (all-matches ...) by #'cddr do ...)
Arc has "pair", which is easily defined in Racket.
(pair '(2 3 4 5 6))
=> '((2 3) (4 5) (6))
(pair '(2 3 4 5 6) +)
=> '(5 9 6)
(define text "abcdefghijklmnop")
(pair '(0 2 4 7 10 15) (curry substring text))
=> '("ab" "efg" "klmno")
The definition:
(define (pair xlist [func list] [accum '()])
(match xlist
[(list a b more ...) (pair more func (cons (func a b) accum))]
[(list a) (reverse (cons (func a) accum))]
[_ (reverse accum)]))