[plt-scheme] Making a fast list like sequence

From: David Van Horn (dvanhorn at ccs.neu.edu)
Date: Tue Apr 7 18:32:33 EDT 2009

I see dramatic differences in running times between the built-in list 
sequence and a naive implementation of list sequencing.  What can I do 
to make this naive implementation run faster?  (I have a cons-like 
structure I want to provide a prop:sequence for, but I would like it to 
be nearly as fast as the real cons if possible).


#lang scheme
(define ls (build-list 100000 (lambda (i) i)))

(define (ls->seq ls)
    (lambda ()
       (compose not empty?)
       (lambda (p) true)
       (lambda (p v) true)))))

(define seq (ls->seq ls))

(time (for ([i seq]) (void i)))

(time (for ([i ls])  (void i)))

cpu time: 2009 real time: 2032 gc time: 0
cpu time: 685 real time: 700 gc time: 0

Posted on the users mailing list.