In Scheme, the namespace of the variables holding functions are the same as that of the regular variables, in CL it isn't. But (define zero-arguments () stuff) zero-arguments won't presumably won't evaluate, just returns the function, as it should, because that is what it is. I think that is what he means, that the variable changes allong with other variables.
He means to ask if you can make variables depend on other variables The following will do so locally:
- Code: Select all
(defmacro let-follow (vars &body body)
"Variables that act like functions."
(let*((functions ;;Paired functions. (looking into them to see which gensym.)
(mapcar (lambda (var &key (gs (gensym)))
`((,gs () ;;Function to get variable.
(or ,gs ,(cadr var)))
((setf ,gs) (to) ;;Function to set variable.
(setf ,gs to))))
(genvars (mapcar #'caar functions)))
`(let*(,@genvars);Make the symbols refer to the functions.
(declare (ignorable ,@genvars))
(symbol-macrolet (,@(mapcar (lambda (var fun)
`(,(car var) (,(caar fun))))
;Flatten out functions into labels. (unlike flet these are allowed to depend on eachother.)
(labels (,@(mapcan #'identity functions))
(declare (ignorable ,@(mapcan (lambda (var)
(function (setf ,var))))
(let-follow ((x 0) (y 5) (z (+ x y)))
(print z) ;;Weirdly enough setq works, guessing symbolmacros change to counter potential confusion.
(print (setq x 3))
Which just makes (setf-able)local functions, and then uses a local symbol-macrolet to turn the symbols into that function. Setting them overrides the function forever-after, and it isn't particularly efficient, but there you go.
To be honest, it doesn't seem particularly useful to me. It is much better to learn how to use FLET and LABELS themselves. It promotes destructive programming too much, and if it is a function, why not call it as such? (Auto-memoization is sortah another topic, btw.) This might be handier for special variables, but there too, you can probably get what you want in functions.
Edit: we might both be misinterpreting you though.. why the '=' mixed with the CL code, do you mean that you want a syntax more like other languages mixed with lisp? I do think that might be a good idea, for instance ML/Haskell-like.. i'd make it but i can't find how to conveniently get the argument list.