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

``Update functions'' in Scheme.



Date: Fri, 8 Apr 88 17:55:27 +0200
From: Oliver Laumann <hplabs!pyramid!tub!net at rutgers.edu>
To:   scheme-request at mc.lcs.mit.edu
Re:   ``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)

accordingly.  Of course, in implementations that support ``fluid-let''
one would also have to extend fluid-let to accept the new syntax.

But how should users be able to define their own ``update functions''?
Yes, I know the concept of ``settable operations'' of T, but unfortunately
they are based on T's object oriented features.  And I don't like the
setf/defsetf concept of Common Lisp, because the accessor function and
the update function have to be separate functions (if I understood it right).

Consider the following example: 

I would like to be able to write a ``print-length'' procedure which,
when called outside a set! form, returns the current ``print length''
(whatever this may be), and, when called as ``(set! (print-length) 10)'',
sets the print length to 10.  Ideally, only *one* function should be
involved -- this function both knows how to return the current print
length and how to set it to a new value.

I could imagine something like this:

	(define print-length
	  (let ((len 100))
	    (lambda (&update flag &optional val)
	      (if flag
		  ...     ; set len to val (possibly with sanity-check)
		  len))))

When called from within a set! form (that is, as an update function),
``flag'' would be true, otherwise false (called as accessor function).
Now I can't come up with a satisfying syntax; the above &update kludge
should only illustrate the concept (i.e. the function "knows" if it
should act as accessor or update function).

A coworker suggests adding a different lambda (which he jokingly
called ``mikro'') that contains two parameter lists and two body
forms, e.g.

	(define print-length
	  (let ((len 100))
	    (mikro () len			          ; accessor
                   (val) (something a la set! len val)))) ; updater


Does anybody else think that a feature like this would be useful?
What kind of syntax (not involving ugly &foo keys -- Scheme currently
does not yet have any of them) could actually be used for this?

--
    Oliver Laumann, Technical University of Berlin, Germany.
    ...!pyramid!tub!net   or   net@TUB.BITNET
    ...!mcvax!unido!tub!net