0

Common-lisp seems to provide "generalized variables" to allow programmatic access to places in addition to names (of variables). As a simple example,

(setf (get 'object :property1) (1+ (get 'object :property1)))

increments the value at the place (get 'object :property1). However, there are also destructive macros like (incf (get 'object :property1)) that can do the same thing more concisely, as the place only needs to be mentioned once. The same pattern occurs whenever the new value is a function of the old value at some place. When the place accessor is complex, the redundancy can get verbose and difficult to readily grasp (at least for me). I would like to know a macro schema for writing my own destructive macros for changing such values, when the new value is some function of the old value. I note that it isn't a simple matter of passing in a function, since the accessor may be embedded in an arbitrary number of function calls or other lisp object.

davypough
  • 1,847
  • 11
  • 21
  • It seems to me that what you want is a [`setf-expander`](http://stackoverflow.com/a/11458640/861493) – anonymous Jan 24 '17 at 20:41
  • 1
    You might find these helpful: http://stackoverflow.com/q/17908564/1281433 , http://stackoverflow.com/questions/25160147/how-to-modify-place-with-arbitrary-function, http://stackoverflow.com/questions/19485964/writing-a-destructive-macro-or-function-like-incf . – Joshua Taylor Jan 24 '17 at 20:48
  • @Joshua: Thank you for the references and explaining the interesting capabilities of define-modify-macro. Can you tell me if I'm at least on the right track by looking at an example like (define-modify-macro deletef (&rest args) (lambda (seq item &key count ...) (delete item seq :count count ...))), where the ellipsis is for the other keywords of delete. First, will this kind of definition always work as intended (where the first argument to deletef is the sequence), and second, how do you effectively include all the keyword arguments? Thanks again... – davypough Jan 26 '17 at 02:47
  • Or maybe this would be a better schema to follow? (define-modify-macro deletef (&rest args) (lambda (seq item &rest args) (apply #'delete item seq args))) – davypough Jan 26 '17 at 05:40
  • @davypough Alexandria has a `DELETEF` macro (pretty much the same as your second one). – jkiiski Jan 26 '17 at 17:14

0 Answers0