Created
May 29, 2012 12:40
-
-
Save geoffrasb/2828184 to your computer and use it in GitHub Desktop.
eval and macro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>(defmacro add-to-list (a lst) | |
`(setf ,lst (cons ,a ,lst))) | |
>(setf lst '(1 2)) ; lst是一個變數,值為(1 2)這個list | |
>(setf a 'lst) ; a是一個變數,值為#:LST這個symbol | |
>(cons 3 lst) ;lst先被evaluate | |
(3 1 2) | |
>lst | |
(1 2) ;lst的值不會變 | |
>(add-to-list 3 lst) ;l拿進macro裡面才被evaluate | |
(3 1 2) | |
>lst | |
(3 1 2) | |
>(eval `(add-to-list 4 ,a)) ;先展開backquote變成(eval '(add-to-list 4 lst)) | |
(4 3 1 2) | |
>lst | |
(4 3 1 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment