[racket] setf in scheme

From: Eli Barzilay (eli at barzilay.org)
Date: Tue Jan 4 06:10:43 EST 2011

On Thursday, Sam Tobin-Hochstadt wrote:
> On Thu, Dec 30, 2010 at 10:34 AM, Razvan Rotaru <roti_343 at yahoo.com> wrote:
> >
> > I was just wondering whether there is anything like setf in
> > scheme. As far as I know there isn't, so I'm asking you, the more
> > experienced schemers out there. If not, why? Could it be crafted
> > with macros?
> The Swindle collection of impressive macros provides a generalized
> `set!'.  See
>   http://docs.racket-lang.org/swindle/
> and
>   http://barzilay.org/Swindle/turbo-doc.html#set!

And it's possible to use it by itself (via `swindle/setf').

On Thursday, Neil Van Dyke wrote:
> If, however, you want it extensible to new setters, I think you'd
> end up defining matching setting procedures and hanging them off the
> getter procedures as (depending on what your Racket or Scheme
> version has) symbol properties, record-type-as-procedure, or some
> similar feature.

The swindle approach is similar to CL so there is no runtime
dispatching, the only difference is that it translates a (set! (foo x)
y) form to (set-foo! x y).  There are some more subtleties involved,
but not much, and it works pretty well.

> Setters like this have sometimes seemed to me to provide a nice
> linguistic symmetry, but at the same time, they are growing the
> language quite a bit despite the appeal of simplicity.  In practice,
> I have never wanted to have setters.  IMHO.

It's not that complicated, and it's something I wish I had every time
I write some `push!'-like piece of code -- and that's one example
where a generalized `set!' works very well in making a simple macro
work in all cases that `set!' is happy with.  (It doesn't happen too
often, but it's enough to be annoying...)

On Thursday, Neil Van Dyke wrote:
> At (at least) a syntactic level, it's making what were simple getter
> procedures somehow be usable as both getters and setters, and I
> think that complicates the language.  Or changes it substantially.

> However, I suspect that implementing this linguistic complication in
> Racket is easy.  I would first try "prop:procedure" in the
> documentation, and a simple notion of lvalues that is used only by
> "set!".

IME, this kind of dispatching doesn't work well for various reasons.
(One that I remember is that people tend to expect the language to
operate on lvalues instead of plain values, whereas the syntactic
approach is limited in a way that is easier to understand.)

          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                    http://barzilay.org/                   Maze is Life!

Posted on the users mailing list.