[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

``Update functions'' in Scheme.

> Did anybody already think about adding ``generalized functions''
> (a la Common-Lisp's setf/defsetf feature) to Scheme?  This would
> eliminate the need for several primitive procedures, among them
> set-car!, set-cdr!, vector-set!, and string-set!.  Instead of writing
>    (set-car! p 5)    or    (vector-set! v 10 'a)
> this would make it possible to write
>    (set! (car p) 5)  or    (set! (vector-ref v 10) 'a)

What's so great about SETF?  It doesn't eliminate the *need* for
the setting primitives (after all, they conceptually must still 
be there); it just makes it easier to remember their "names"
by providing a convention for deriving the setter from the

But it's easy to develop alternate naming conventions without
altering Scheme.  One simple convention can be carried out
by simple renaming:

  (define set!car set-car!)
  (define set!cdr set-cdr!)
  (define set!vector-ref vector-set!)
  (define set!string-ref string-set!)

Here the name of each setter is the name of the accessor prefixed
by set!  Now we can write

  (set!car p 5)    or   (set!vector-ref v 10 'a)

which, except for the missing pair of parens, looks a lot like
the SETF approach.  And we didn't have to extend Scheme all!

- Lyn -