The less-familiar parts of Lisp for beginners — define-symbol-macro

Another feature of Lisp that the newcomer might not have encountered is define-symbol-macro.  Think of this as something very close to a C/C++ macro.  It is a way to define a symbol that will be expanded at compile time.  Where the more familiar Lisp macros have a function-like syntax, this creates a macro with a symbol-like syntax, so it is called without surrounding parentheses.

Here’s a short bit of code to demonstrate this:
 

(define-symbol-macro now (get-time-of-day))
(define-symbol-macro first-fred (car fred))

(defun demonstrate ()
  (format t "The value of symbol 'now' is: ~A~%" now)
  (let ((fred (list 1 2 3 4 5)))
    (format t "The value of symbol 'first-fred' is ~A~%" 
            first-fred)
    (let ((fred (list :A :B :C :D :E)))
      (format t "The value of symbol 'first-fred' is ~A~%" 
              first-fred))))

With output:
 
CL-USER> (demonstrate)
The value of symbol 'now' is: 1388450944
The value of symbol 'first-fred' is 1
The value of symbol 'first-fred' is A
NIL
CL-USER> (macroexpand 'now)
(GET-TIME-OF-DAY)
T
CL-USER> (macroexpand 'first-fred)
(CAR FRED)
T

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

反垃圾邮件 / Anti-spam question * Time limit is exhausted. Please reload CAPTCHA.