0

Suppose that the following macro execution:

(make-model '((1 2)(3 4)(5 6)(7 8)))

, can generate the following list:

((MAKE-INSTANCE 'MODEL :NAME 7 :ID 8) 
 (MAKE-INSTANCE 'MODEL :NAME 5 :ID 6)
 (MAKE-INSTANCE 'MODEL :NAME 3 :ID 4) 
 (MAKE-INSTANCE 'MODEL :NAME 1 :ID 2))

If I store the result in a parameter (e.g *test*), how could I get lisp to execute the four commands in the list?

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
Xaving
  • 329
  • 1
  • 11

1 Answers1

3

You can use eval:

(mapcar #'eval *test*)

However, this is a suboptimal solution.

You would be much better off saving either a lambda:

(defmacro make-model-lambda (args)
  (list* 'lambda () (apply #'make-model args)))
(defparameter *test* (make-model-lambda ....))
(funcall *test*)

or just the list of args themselves:

(defparameter *test*
  (mapcar (lambda (name-id)
            (list 'model :name (first name-id) :id (second name-id)))
          '((1 2)(3 4)(5 6)(7 8))))
(mapcar #'apply *test*)
Community
  • 1
  • 1
sds
  • 58,617
  • 29
  • 161
  • 278
  • Ok thank you. I think I understand the principle now. – Xaving Feb 03 '15 at 14:39
  • I'm a little lost regarding the dot notation inside the backquote form. isnt that going to result in `(lambda () make-model ..whatever-args-evald-to))` ? or is it meant to be a comma? – Baggers Feb 09 '15 at 08:30