For a program, I'm trying to use a macro such as this one to simplify my code downstream and avoid repeating the same code again and again:
(defmacro destructure (values &body body) `(let* ((other-values (rest values)) (age (getf other-values :age)) (len (getf other-values :len)) (all (getf other-values :all))) (progn ,@(loop for e in body collect `(,@e))))) It is supposed to work with a list such as this:
'(name :age 1 :len 2 :all '(1 2 3 4 5)) The idea is that I should be able to run this code:
(destructure '(name :age 1 :len 2 :all '(1 2 3 4 5)) (type-of age) (first all)) Or, using a variable, like this:
(setf *values* '(name :age 1 :len 2 :all '(1 2 3 4 5))) (destructure *values* (type-of age) (first all)) Instead of having to access the different elements each time. Of course this is a simplified example, but the list that I have to work on is reality is much longer.
I'm finding it extremely hard to do this. Basically, the code above does not work (unless I cheat by setfing a global variable "values" to hold my list) because the macro is treating the symbol "values" just as that, without expanding the list that it should point to.
On the other hand, I can't use a regular function because then the instructions I'm passing in the body are immediately executed, but I have to place them in the let.
I'm new enough to the language and I believe that I'm probably missing something, and that there probably is a way to achieve it. Is there?