Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a list to macro in common lisp?

Tags:

common-lisp

I'm trying to pass a list to macro, for example:

(defmacro print-lst (lst)
  `(progn
     ,@(mapcar #'(lambda (x) `(print ,x)) lst)))
(let ((lst '(1 2 3)))
      (print-lst lst))

It caught error: "The value LST is not of type LST".

So, my question is, what's wrong with this piece of code and how to pass list to macro?

like image 864
Wei Li Avatar asked Jan 27 '26 15:01

Wei Li


1 Answers

I'm not sure why you want to define this as a macro instead of a regular function, but the problem is that macros do not evaluate their arguments. If you give it the name of a lexical variable, all it sees is the name ('LST), not the bound value. It is complaining (correctly) that the symbol 'LST is not a list, and thus not a valid second argument to MAPCAR.

You could call it as (print-lst (1 2 3)), but then you could do without the macro and just do (mapc #'print lst)

like image 146
finnw Avatar answered Jan 30 '26 22:01

finnw