I'm trying to learn Lisp but I got stuck by this example (you can find it on "ANSI Common Lisp", by Paul Graham, page 170):
(defmacro in (obj &rest choices) (let ((insym (gensym))) `(let ((,insym ,obj)) (or ,@(mapcar #'(lambda (c) `(eql ,insym ,c)) choices))))) Graham then states:
The second macro [...]
inreturnstrueif its first argument iseqlto any of the other arguments. The Expression that we can write as:
(in (car expr) '+ '- '*) we would otherwise have to write as
(let ((op (car expr))) (or (eql op '+) (eql op '-) (eql op '*))) Why I should write a macro when the following function I wrote seems to behave in the same way?
(defun in-func (obj &rest choices) (dolist (x choices) (if (eql obj x) (return t)))) I do not understand if I am missing something or, in this case, in-func is equivalent to in.
dolistis also a macro, albeit a more complicated one.dolistis a macro, but in general there is a good point in avoiding writing new macros if you can do the same with functions. Macros can make code harder for future readers since they don't follow the usual evaluation rules. (But of course in this caseinis defined as a macro exactly because of that.)