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

From: Joe Marshall (jrm at ccs.neu.edu)
Date: Thu May 27 11:21:02 EDT 2004

Doug Orleans <dougo at place.org> writes:

> 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 don't see an easy way.




Posted on the users mailing list.