[plt-scheme] Does anyone use `set!' and `get!' patterns?

From: hendrik at topoi.pooq.com (hendrik at topoi.pooq.com)
Date: Wed Mar 12 18:39:55 EDT 2008

On Wed, Mar 12, 2008 at 05:03:19PM -0400, Sam TH wrote:
> On Wed, Mar 12, 2008 at 4:21 PM,  <hendrik at topoi.pooq.com> wrote:
> >
> > On Tue, Mar 11, 2008 at 09:59:13AM -0400, Sam TH wrote:
> >  > On Tue, Mar 11, 2008 at 9:36 AM, Eli Barzilay <eli at barzilay.org> wrote:
> >  > > This is probably the original motivation for this feature.  For
> >  > >  example, with the generic setters in Swindle, you get
> >  > >
> >  > >   (inc! (cadddr x))
> >  > >
> >  > >  to expand to
> >  > >
> >  > >   (set-cadddr! x (+ (cadddr x) 1))
> >  > >
> >  > >  and my guess is that with the match setters, it would be like doing
> >  > >
> >  > >   (let ([x (cdddr x)]) (set-car! x (+ (car x) 1)))
> >  >
> >  > Right.  Something like:
> >  >
> >  > (match x
> >  >   [(list-rest c _ _ (set! x!)) (x! (+ c 1))])
> >
> >  That looks useful!  If, that is, the objects in question are mutable.
> 
> You might think so.  But after at least 5 years, it seems to have
> gotten no use at all.  Do you have any particular uses in mind?

I tend to use immutable data.  When I need to make changes, (such as 
applying rewriting rules) I build a modified copy.  If I have a rule 
like f(x) -> g(x, x) I can avoid copying the x and just refer to it.  
The storage gain can be large.  The coding overhead of maintiaing my 
current position in a large tree, recursing into it to find places to make 
changes, and then recursing out while reconstructing changed trees is 
rather complex -- so complex that I have used a program generator to 
perform all this correctly.

If I were to use mutable data, I'd take a big data-structure allocation 
hit on rules like f(x) -> g(x, x) because I'd have to copy the entire 
tree I've denoted by 'x', but pattern-mathing and replacement would be 
easier to code.  I'd definitely be interestin in modification-in-place 
in such a scheme.

Whether I'd be doing it using a set! in a pattern-matching macro, well, 
I don't know about that.  But the proram-transformation tool I 
implemented and used (not in Scheme) definitely has provision for doing 
replacements inside pattern matches, and this gets heavily used.

-- hendrik


Posted on the users mailing list.