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

set in Scheme?



    Date: 5 May 88 21:58:28 GMT
    From: uhccux!chin at humu.nosc.mil (David Chin)

    Does anyone know how to do the equivalent of a Common Lisp "set" in
    Scheme.  In particular, I would like to be able to pass different
    symbols as arguments to a function and then set! or define them inside
    the function.  Is this style of programming (global side-effects for
    function calls) contrary to the Scheme philosophy?

Don't pass the symbol, pass a closure that will assign the variable:

	(foo (lambda (new-value) (set! var new-value)))

If you also need to get the value of the variable, or find out its name,
pass an object that can do any operations that need doing:

	(foo (lambda (operation)
	       (case operation
	         ((value) var)
		 ((set-value!) (lambda (new-value) (set! var new-value)))
		 ((name) 'var))))

Global side effects are not contrary to "scheme philosophy" (although
they are discouraged), but confusing variables with their names is.