Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Macro for more than 1 line of code

I'm learning the macro system of Common Lisp and suddenly found a problem

(defun hello () (format t "hello ~%")) 
(defun world () (format t "world ~%"))
(defmacro call-2-func (func1 func2)
  `(,func1)
  `(,func2))

(macroexpand-1 '(call-2-func hello world)) 
(WORLD) 
T

Well. Why can't I generate 2 LoC from only one macro? How can I work around? (progn will not work in a more complicated situation...)

like image 784
Mike Manilone Avatar asked Jan 15 '23 04:01

Mike Manilone


1 Answers

Your macro needs to return just one form that will call both functions.
Instead you are generating two forms (and only the last one is used.)

Try:

(defmacro call-2-func (func1 func2)
  `(progn (,func1) (,func2)))

or if you do not want to be limited to just 2 functions:

(defmacro call-funcs (&rest funcs)
  `(progn ,@(mapcar #'list funcs)))
like image 185
finnw Avatar answered Jan 31 '23 06:01

finnw