[plt-scheme] should `map' reuse cons cells?

From: Doug Orleans (dougo at place.org)
Date: Thu May 27 10:37:48 EDT 2004

Joe Marshall writes:
 > If you used `hash-consing' you'd get this behavior for free.
 >   (define *the-conses* (make-hash-table 'weak 'equal))
 >   (define *the-cell* (cons #f #f))
 >   (define (hcons car cdr)
 >     (set-car! *the-cell* car)
 >     (set-cdr! *the-cell* cdr)
 >     (hash-table-get *the-conses* *the-cell*
 >                     (lambda ()
 >                       (begin0 *the-cell*
 >                         (hash-table-put! *the-conses* *the-cell* *the-cell*)
 >                         (set! *the-cell* (cons #f #f))))))
 >   (define (hmap f l)
 >     (if (null? l)
 >         '()
 >         (hcons (f (car l)) (hmap f (cdr l)))))

Huh, cool, thanks.  Is there a way to do this without `equal?',
though?  I'd rather use `eq?' to compare the contents of the cell, but
with PLT hash tables you either get shallow or deep compare, nothing
in between.  I guess you could make "proxy" pairs of gensyms in an
`equal?'-table to match with the real pairs in an `eq?'-table, but is
there a better way to do it?

--dougo at place.org

Posted on the users mailing list.