Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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)


Download ppt "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."

Similar presentations


Ads by Google