Lets say I have two variables and I want to set the variable with lower value to nil.
Is it possible to make it work this way?
(setf a1 5)
(setf a2 6)
(setf
(if (< a1 a2) a1 a2)
nil
)
If you want to do something close to this, you can use (setf (symbol-value var) ...)
:
> (defparameter a1 5)
> (defparameter a2 6)
> (setf (symbol-value (if (< a1 a2) 'a1 'a2)) nil)
> a1
nil
> a2
6
To get a syntax closer to the one in your question, you can define an setf-expander
for if
:
(defsetf if (cond then else) (value)
`(progn (setf (symbol-value (if ,cond ,then ,else)) ,value)
,value))
Then you can write (setf (if (< a1 a2) 'a1 'a2) nil)
However, the best way of writing the code is probably to do it straight forward, using an if
with setf
forms in both branches:
(if (< a1 a2)
(setf a1 nil)
(setf a2 nil))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With