[plt-scheme] should `map' reuse cons cells?
At Thu, 27 May 2004 11:21:02 -0400, Joe Marshall wrote:
> For list-related administrative tasks:
> http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
> 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.
If you are sure that the only arguments to hcons are either null or the
results of other hcons, you can probably get a scheme based on eq? to
work (you could even check this as a contract).
Robby