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

Re: ``Update functions'' in Scheme.



In article <8805031911.AA04352@basel>, lyn@BASEL (Franklyn Turbak) writes:
> > 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'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!) [...]
>
>   (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!

I don't know about Common Lisp's SETF, but in the T dialect of Scheme there is a
difference between the two.  When you write (SET (FOO ...) ...), the operation
FOO can specify how it is to be set, i.e., it can handle the operation (SETTER
FOO) and return a setter procedure, which then does the setting appropriately.
This is not a matter of simple renaming; rather, it is a very useful programming
construct which lets you define new operations and their setters within the
existing syntax.

For example, you could implement a two-dimensional array by defining an accessor
operation (array-element A x y), where the definition of the operation would
include a definition of its setter.

Of course, you can do this without this feature too; it just turns out to be a
nice way to program, that's all.

-- Ashwin.

ARPA:    Ram-Ashwin@cs.yale.edu
UUCP:    {decvax,ucbvax,harvard,cmcl2,...}!yale!Ram-Ashwin
BITNET:  Ram@yalecs