Download presentation
Presentation is loading. Please wait.
Published byFranklin McCoy Modified over 8 years ago
1
Macros Forms that the compiler expands into code ● Decide if the macro is really necessary ● Write down the syntax of the macro ● Figure out what the macro should expand into ● Use defmacro to implement
2
syntax and expansion Example: (while test body) (do ((dummy nil nil)) ((not test)) body)
3
Defining the macro (defmacro while (test &rest body) “Repeat body while test is true.” (list* 'do '((dummy nil nil)) (list (list 'not test)) body))
4
Expanding the macro (macroexpand-1 '(while (< i 10) (print (* i i)) (setf i (+ 1 i)))) (DO ((DUMMY NIL NIL)) ((NOT (< I 10))) (PRINT (* I I)) (SETF I (+ 1 I)))
5
Backquote Notation (defmacro while (test &rest body) (let ((code '(do ((dummy nil nil)) ((not test)). body))) (subst test 'test (subst body 'body code))))
6
Backquote “`” is like quote “'”, but knows that what follows may contain some components that rae to be evaluated (defmacro mwhile (test &rest body) `(do ((dummy nil nil)) ((not,test)),@body))
7
Some examples (setf test1 '(a test)) => (A TEST) `(this is,test1) => (THIS IS (A TEST)) `(this is.,test1) => (THIS IS A TEST) `(this is,@test1) => (THIS IS A TEST)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.