[plt-scheme] Sequences are no fun
I'm trying to code up some higher-order functions on sequences, and
I'm finding this to be quite tricky. A big part of the problem is
that there is no good way to "peek" at the head of the sequence
without consuming it, and another big part of the problem is the sheer
verbosity of converting sequences to generating functions and back.
As a case in point, here's what I've go so far for drop-while:
(define (sequence->list seq) (for/list ((i seq)) i))
(define (identity x) x)
(define (const-true x) #t)
(define (drop-one seq)
(let-values ([(more? next) (sequence-generate seq)])
(next)
(make-do-sequence
(λ () (values identity (λ (x) (next)) (next) const-true
const-true (λ (t v) (more?)))))))
(define (drop-while pred seq)
(let-values ([(more? next) (sequence-generate seq)])
(cond
[(not (more?)) empty]
[else (let ([next-value (next)])
(cond
[(pred next-value) (drop-while pred (drop-one seq))]
[else (make-do-sequence
(λ () (values identity (λ (x) (next))
next-value const-true const-true (λ (t v) (more?)))))]))])))
This almost works, but a sequence formed by drop-while isn't consumed
properly by sequence->list (try doing a sequence->list twice on
something generated by drop-while, and you'll see what I mean).
Is there a better way to manipulate sequences, or is it always as
grueling as this? Is there a more elegant way to do this that I'm
missing?
--Mark