For better understanding
if you have a macro:
Code: Select all
(defmacro add10-m (num)
`(+ 10 ,num))
you can also have a function with quasiquotation:
Code: Select all
(defun add10-f (num)
`(+ 10 ,num))
The difference is that macros are evaluated in
compile-time and get unevaluated arguments. Back to the aforedefined function, the function can be called like this:
or more clearly:
and it returns:
But we give it only data and get data from it too, coincidentally they look like code (on the input side we reached it with
quote -- that apostrophe). If we use a macro instead of a function for example:
The
compiler invokes the macro function and passes the calling form* to that function (it's nicely parsed, due to the lambda-list in a defmacro form) so we don't quote, it does the compiler.
The macro function returns something which is expected to be code which is processed by the compiler further and afterwards the result of compilation is evaluated during
run-time.
The macro can be written with the help of our function:
Code: Select all
(defmacro add10-m (num)
(add10-f num))
Maybe will help you the information: in CL beside macros exist
reader macros which take part during
read-time, the key is to realise differences among
run-time,
compile-time and
read-time. At least for me it was the key, but reader macros are beyond this single post.
Rather try this sample:
Code: Select all
(defmacro some-value () (read))
(defun add (num) (+ num (some-value))
(add 10)
After typing
defun form (insert the code into REPL line by line) it will hang because of
read so type some lisp form which evaluates to number and try the new defined function; it won't hang because there is no
read in that function.
*Compare
function lambda-list and
macro one (for example &whole keyword).