際際滷

際際滷Share a Scribd company logo
Macro & compilation
Ikhoon
Introduction
 Macro
 語広
 Macro function or 譴 macro
 
 Lisp syntax襯 ロ  覦覯
 Tool
 PPMX(pretty print macro expansion)
 Compiler
 
 Lisp襦蠏碁 蠍郁語企 覦蠑碁 
 螻
 朱朱 10 ~ 100覦  焔 レ
Macro as shorthand
 (INCF A) vs. (SETQ A (+ A 1))
 襷ろ襦 覲旧″  螳牛蟆   
 SETF vs. SETQ
 襷ろ襦 SETF, INCF special  SETQ覲企 smart
 (incf (aref (nth array-num *list-of-arrays*)
(first subscripts)))
 DEFSTRUCT 襷ろ襦
 MAKE-STARSHIP, STARSHIP-P, STARSHIP-NAME  
襯 覓旧朱
Macro expansion
 Lisp Macro 朱  
  語襯 螳讌  曙  
 > (ppmx (incf a))
Macro expansion:
(SETQ A (+ A 1))
 > (ppmx (incf a))
Macro expansion:
(LET ((#:G0144 (+ A 1)))
(SETQ A #:G0144))
Defining a macro
 DEFMACRO
 襷ろ襦 defmacro襯 伎伎 
 Syntax defun螻 
 Macro  螳 讌    襷れ 覦
 螳 覯 incf
 list襯 伎伎 lisp 螳  
 (defmacro simple-incf (var)
(list setq var (list + var 1)))
(setf a 4)
> (simple-incf a)
5
 (defmacro simple-incf (var &optional (amount 1))
(list setq var (list + var amount)))
(setf b 2)
> (simple-incf b (* 3 a))
17
Why macro?
 Function or MACRO
 INCF  襷ろ襦襦襷 襷  .
 覯 incf
(defun faulty-incf (var)
(setq var (+ var 1)))
(setf a 7)
> (faulty-incf a)
8
> (faulty-incf a)
8
> a
7 ;; a 螳 讀螳 讌 
 語 螳 襦 れ願蠍  螳伎
 讀螳 螳 伎 讌 覲 var伎 a螳
Macros as syntactic extensions
 Macro 覈
 Lisp Language syntax
 Macro vs. Function
1. Function 語  螳 . Macro 語 螳
讌 .
2. Function 蟆郁骸螳 企 蟆企讌   . Macro
覦 螳 覦  Lisp 伎伎 .
3. Macro螳 覦  讀 螳 讌. Function
覦 蟆郁骸 螳 讌讌 .
Macros as syntactic extensions
 Special functions
 SETQ, IF, LET, BLOCK ..
 Common lisp 螳  覯 觚襦
 Scoping, block 螻 loop螻 螳 蠍磯蓋 願規譟磯ゼ 豈
讌
 Macro 襷谿螳讌襦 語れ 螳讌 
 螳蠍   覦 
 襦 special function   , lisp implementer襷
The backquote character
 Backquote [ ` ]
 Quote 蟆 list襯 語蠍 伎 
 Unquote
 Comma [ , ]
 Backquote list 企 螳  comma螳 覿蟆 覃 unquote
 Unquote 一讌  豌願 螳 蠏瑚 螳 覩
(setf name fred)
> `(this is ,name from pittsburgh)
(THIS IS FRED FROM PITTSBURGH)
> `(i gave ,name about ,(* 25 8) dollars)
(I GAVE FRED ABOUT 200 DOLLARS)
 Simple-incf
 (defmacro simple-incf (var) ;; list襯  襷ろ襦
(list setq var (list + var 1)))
 (defmacro simple-incf (var &optional (amount 1)) ;; backquote襯  襷ろ襦
`(setq ,var (+ ,var ,amount)))
Splicing with backquote
 Splice
 Comma-at [ ,@ ]
 ,@ 伎 蠍磯 蟆郁骸 螳  曙 蟆  
(spliced)讌
 ,@ 螳 瑚 蟯瑚  讌  蠍 覓語 覦
list襷 伎 
(setf name fred)
(setf address (16 maple drive))
> `(,name lives at ,address now) ;;
Inserting.
(FRED LIVES AT (16 MAPLE DRIVE) NOW)
> `(,name lives at ,@address now) ;;
Splicing.
(FRED LIVES AT 16 MAPLE DRIVE NOW)
The compiler
 Function compilation
 COMPILE 企朱 覈轟企ゼ 牛伎 螳
 File compilation
 COMPILE-FILE 企朱 覈轟企ゼ 牛伎   
 焔
 10 ~ 100覦一 レ 螳
Compiling entire programs
 覃讌 豌襴
 Global variable
 Global variable 覃 assumed to be SPECIAL 企朱 蟆所
覓瑚規螳 豢ル
 DEFVAR, DEFPARAMETER, DEFCONSTANT襯 蟆 伎
 蟆所 覃讌襯   
 語 殊 豐覦覿 OR 蠏 螳 谿語^   碁伎

 MACRO
 襷ろ襦 語 覦 企 螳 蠏瑚 谿語^蠍  
 
  foo 螳 襷ろ襦 bar襯 覿襯  bar螳 foo覲企 れ る foo襯
貉危   襷ろ襦 bar襯 ロ伎  蟆 讌 覈詩
 Built-in 
 伎ロ  覃 貉危 螳
Advanced topics:
The &body lambda-list keyword
 WHILE loop in lisp
 (defmacro while (test &body body)
`(do ()
((not ,test))
,@body))
 襷ろ襦襯 牛伎 襦 syntax襯 豢螳
 &body
 &body &rest 狩 蠍磯
 蟆讌 語れ list 襦 伎
 襷ろ襦襯 曙  襾語 覿覿れ Lisp code body覿覿
 る 蟆 れ
Advanced topics:
Destructuring lambda lists (1)
 Destructuring
 襷ろ襦 レ語襯 螳  
   朱 覿襴  襦 List 豌 豬蠍 螳
 襷ろ襦 螳
 覲旧″ syntax  蟲譟一 
 (defmacro mix-and-match (p q) ;; not destruturing
(let ((x1 (first p))
(y1 (second p))
(x2 (first q))
(y2 (second q)))
`(list (,x1 ,y1)
(,x1 ,y2)
(,x2 ,y1)
(,x2 ,y2))))
 (defmacro mix-and-match ((x1 y1) (x2 y2)) ;; destruturing
`(list (,x1 ,y1)
(,x1 ,y2)
(,x2 ,y1)
(,x2 ,y2)))
Advanced topics:
Destructuring lambda lists - example
 DOVECTOR
 (defmacro dovector ((var vector-exp
&optional result-form)
&body body)
`(do* ((vec-dov ,vector-exp)
(len-dov (length vec-dov))
(i-dov 0 (+ i-dov 1))
(,var nil))
((equal i-dov len-dov) ,result-form)
(setf ,var (aref vec-dov i-dov))
,@body))
> (dovector (x #(foo bar baz))
(format t "~&X is ~S" x))
X is FOO
X is BAR
X is BAZ
NIL
 vec-dov, len-dov, i-dov  豐蠍郁 願鍵  讌 覲
 覲 企 豢 襷蠍 伎 package system螻 gensyms襯  蟆 覦讌讌襷
 豈 覯襯 覯  伎
?
Advanced topics:
Macros and lexical scoping
 襦 incf 襷り鍵
 螳 螳 蟆 狩蠍  symbol 
 覲  Read & Update螳 螳ロ伎 
 覲 螳
 (defun faulty-incf (var)
(set var (+ (symbol-value var) 1)))
(setf a 7)
> (faulty-incf a)
8
> (faulty-incf a)
9
> a
9
 讌 覲 襷ろ襦襷 螳
 (defun test-simple (turnip)
(simple-incf turnip))
(defun test-faulty (turnip)
(faulty-incf turnip))
> (test-simple 37)
38
> (test-faulty 37)
 Error: TURNIP unassigned variable.
 FAULTY-INCF 覿覈 lexical-context global-context願鍵 覓語 TEST-FAULTY 讌覲 TURNIP lexically
蟆 蠏殊 伎讌 .
Advanced topics:
Dynamic scoping
 Lexical scoping
 
 X覲 蠏狩蠍 伎 X螳 伎 body 襷 蠏手
 
 DEFUN朱  伎
  local 覲  覲 蠏 螳
 讌
  BAR lambda  朱 
  覲, BAR 覲  覲 蠏 螳
 Dynamic scoping
 Dynamic variable Special variable企手 覿襴
 
 覲 X螳 special襦 碁覃 るジ  讌 覲螳   螻
企 蠏殊 螳
 DEFVAR 襷ろ襦襯 牛
Advanced topics:
Dynamic scoping - example
 (defvar birds) ;; bird襯 special襦 
(setf fish (salmon tuna)) ;; fish  lexical
(setf birds (eagle vulture)) ;; birds  special
(defun ref-fish () ;; 谿語^  
fish)
(defun ref-birds ()
birds)
(ref-fish) => (salmon tuna) ;; top level ろ
(ref-birds) => (eagle vulture)
 (defun test-lexical (fish)
(list fish (ref-fish)))
> (test-lexical (guppy minnow)) ;; lexical 覲 谿語^
((GUPPY MINNOW) (SALMON TUNA)) ;; ref-fish  fish lexical scoping 磯  覲 谿語^
 (defun test-dynamic (birds)
(list birds (ref-birds))) ;; dynamic 覲 谿語^
> (test-dynamic (robin sparrow)) ;; ref-birds   birds dynamic 覲 bird 谿語^
((ROBIN SPARROW) (ROBIN SPARROW))
 > (ref-birds)
(EAGLE VULTURE)
 TEST-DYNAMIC body襦 れ 螳  襦 dynamic 覲 birds螳 蠍郁 TEST-DYNAMIC   蟾讌 覲
birds 願 谿語^ 蟆
Advanced topics:
defvar, defparameter, defconstant
 DEFVAR
 豐蠍 螳     
 (defvar *total-glasses*)
 覯 蠏 螳 碁覃 defvar襯 牛伎 覦蠖  
> (defvar *total-glasses* 0
"Total glasses sold so far")
> (defvar *total-glasses* 3
"Total glasses sold so far")
> *total-glasses*
0
 DEFPARAMETER
 豐蠍 螳螻 蟷  伎 
 (defparameter abc 10)
 碁 螳 defparameter襯 牛 覦蠖  
> (defparameter *max-glasses* 500
"Maximum number of glasses we can make")
> (defparameter *max-glasses* 300)
> *max-glasses*
300
 defvar defparameter 螳 覓碁 蟲譟一伎襷 defparameter 襦蠏碁 ろ  覲讌   螳 
 DEFCONSTANT
 襷れ伎   覦讌 
 Lisp special valiableれ 蟯旧朱 *襦  語  讌襷 defconstant 碁ゼ 
 Lisp PI 螳 伎 れ
Advanced topics:
Rebinding special variables
 Function 語
  語襯 special 覲 企朱 讌 語  rebinding
(defun print-in-base (*print-base* x)
(format t "~&~D is written ~S in base ~D."
x x *print-base*))
> (print-in-base 2 205)
205 is written 11001101 in base 2.
NIL
 LET
 LET 牛 覦碁
(defvar *foo* 2)
(defun bump-foo ()
(incf *foo*))
(defun rebind-boo ()
(let ((*foo* 100))
(incf *foo*)
(bump-foo)))
Ansi Lisp:
Macro design  example(1)
 ntimes 襷ろ襦 襷り鍵
 ル 襷 覦覲 ろ 襷ろ襦
 (ntimes count &body body)
 > (ntimes 10
(princ "."))
..........
NIL
 References
 ansi lisp
 http://onlisp.blogspot.com/2007/12/10-ansi-common-lisp.html
Ansi Lisp:
Macro design  example(2)
 ntimes 襷ろ襦
 (defmacro ntimes (n &rest body)
`(do ((x 0 (+ x 1)))
((>= x ,n))
,@body))
 覓語
 覲 x襯 燕伎 蠍一ヾ 覲 x螳 る 豺  蟆郁骸 覦
 覈視 
 (let ((x 10))
(ntimes 5
(setf x (+ x 1)))
x)
10
 X 10 麹螻 蠏 螳 5覯 讀螳 貅 讌襷 蟆郁骸 螳 蠏碁襦 10 
 襷ろ襦 
 (let ((x 10))
(do ((x 0 (+ x 1)))
((>= x 5))
(setf x (+ x 1))) ;; let朱 焔 x螳  do 覲 x螳 讀螳
x)
Ansi Lisp:
Macro design  example(3)
  ntimes 襷ろ襦
 do loop 覲襯 gensym 牛 襦蠏碁伎 企 覲手骸 螳讌 蟆 襷
 (defmacro ntimes (n &rest body)
(let ((g (gensym)))
`(do ((,g 0 (+ ,g 1)))
((>= ,g ,n))
,@body)))
 覓語
 覦覲 螳
 do 覓語 body螳 螳  襷 n 襦蟆 螳 .
 覈視 
 > (let ((v 10))
(ntimes (setf v (- v 1))
(princ ".")))
.....
NIL
  蟆郁骸 v 10 1 觝 9螳  豢
 襷ろ襦 
 (let ((v 10))
(do ((#:g1 0 (+ #:g1 1)))
((>= #:g1 (setf v (- v 1))))
(princ ".")))
Ansi Lisp:
Macro design  example(4)
  ntimes 襷ろ襦
 (defmacro ntimes (n &rest body)
(let ((g (gensym))
(h (gensym)))
`(let ((,h ,n))
(do ((,g 0 (+ ,g 1)))
((>= ,g ,h))
,@body))))
 覓語
 願屋

More Related Content

Macro & compilation

  • 2. Introduction Macro 語広 Macro function or 譴 macro Lisp syntax襯 ロ 覦覯 Tool PPMX(pretty print macro expansion) Compiler Lisp襦蠏碁 蠍郁語企 覦蠑碁 螻 朱朱 10 ~ 100覦 焔 レ
  • 3. Macro as shorthand (INCF A) vs. (SETQ A (+ A 1)) 襷ろ襦 覲旧″ 螳牛蟆 SETF vs. SETQ 襷ろ襦 SETF, INCF special SETQ覲企 smart (incf (aref (nth array-num *list-of-arrays*) (first subscripts))) DEFSTRUCT 襷ろ襦 MAKE-STARSHIP, STARSHIP-P, STARSHIP-NAME 襯 覓旧朱
  • 4. Macro expansion Lisp Macro 朱 語襯 螳讌 曙 > (ppmx (incf a)) Macro expansion: (SETQ A (+ A 1)) > (ppmx (incf a)) Macro expansion: (LET ((#:G0144 (+ A 1))) (SETQ A #:G0144))
  • 5. Defining a macro DEFMACRO 襷ろ襦 defmacro襯 伎伎 Syntax defun螻 Macro 螳 讌 襷れ 覦 螳 覯 incf list襯 伎伎 lisp 螳 (defmacro simple-incf (var) (list setq var (list + var 1))) (setf a 4) > (simple-incf a) 5 (defmacro simple-incf (var &optional (amount 1)) (list setq var (list + var amount))) (setf b 2) > (simple-incf b (* 3 a)) 17
  • 6. Why macro? Function or MACRO INCF 襷ろ襦襦襷 襷 . 覯 incf (defun faulty-incf (var) (setq var (+ var 1))) (setf a 7) > (faulty-incf a) 8 > (faulty-incf a) 8 > a 7 ;; a 螳 讀螳 讌 語 螳 襦 れ願蠍 螳伎 讀螳 螳 伎 讌 覲 var伎 a螳
  • 7. Macros as syntactic extensions Macro 覈 Lisp Language syntax Macro vs. Function 1. Function 語 螳 . Macro 語 螳 讌 . 2. Function 蟆郁骸螳 企 蟆企讌 . Macro 覦 螳 覦 Lisp 伎伎 . 3. Macro螳 覦 讀 螳 讌. Function 覦 蟆郁骸 螳 讌讌 .
  • 8. Macros as syntactic extensions Special functions SETQ, IF, LET, BLOCK .. Common lisp 螳 覯 觚襦 Scoping, block 螻 loop螻 螳 蠍磯蓋 願規譟磯ゼ 豈 讌 Macro 襷谿螳讌襦 語れ 螳讌 螳蠍 覦 襦 special function , lisp implementer襷
  • 9. The backquote character Backquote [ ` ] Quote 蟆 list襯 語蠍 伎 Unquote Comma [ , ] Backquote list 企 螳 comma螳 覿蟆 覃 unquote Unquote 一讌 豌願 螳 蠏瑚 螳 覩 (setf name fred) > `(this is ,name from pittsburgh) (THIS IS FRED FROM PITTSBURGH) > `(i gave ,name about ,(* 25 8) dollars) (I GAVE FRED ABOUT 200 DOLLARS) Simple-incf (defmacro simple-incf (var) ;; list襯 襷ろ襦 (list setq var (list + var 1))) (defmacro simple-incf (var &optional (amount 1)) ;; backquote襯 襷ろ襦 `(setq ,var (+ ,var ,amount)))
  • 10. Splicing with backquote Splice Comma-at [ ,@ ] ,@ 伎 蠍磯 蟆郁骸 螳 曙 蟆 (spliced)讌 ,@ 螳 瑚 蟯瑚 讌 蠍 覓語 覦 list襷 伎 (setf name fred) (setf address (16 maple drive)) > `(,name lives at ,address now) ;; Inserting. (FRED LIVES AT (16 MAPLE DRIVE) NOW) > `(,name lives at ,@address now) ;; Splicing. (FRED LIVES AT 16 MAPLE DRIVE NOW)
  • 11. The compiler Function compilation COMPILE 企朱 覈轟企ゼ 牛伎 螳 File compilation COMPILE-FILE 企朱 覈轟企ゼ 牛伎 焔 10 ~ 100覦一 レ 螳
  • 12. Compiling entire programs 覃讌 豌襴 Global variable Global variable 覃 assumed to be SPECIAL 企朱 蟆所 覓瑚規螳 豢ル DEFVAR, DEFPARAMETER, DEFCONSTANT襯 蟆 伎 蟆所 覃讌襯 語 殊 豐覦覿 OR 蠏 螳 谿語^ 碁伎 MACRO 襷ろ襦 語 覦 企 螳 蠏瑚 谿語^蠍 foo 螳 襷ろ襦 bar襯 覿襯 bar螳 foo覲企 れ る foo襯 貉危 襷ろ襦 bar襯 ロ伎 蟆 讌 覈詩 Built-in 伎ロ 覃 貉危 螳
  • 13. Advanced topics: The &body lambda-list keyword WHILE loop in lisp (defmacro while (test &body body) `(do () ((not ,test)) ,@body)) 襷ろ襦襯 牛伎 襦 syntax襯 豢螳 &body &body &rest 狩 蠍磯 蟆讌 語れ list 襦 伎 襷ろ襦襯 曙 襾語 覿覿れ Lisp code body覿覿 る 蟆 れ
  • 14. Advanced topics: Destructuring lambda lists (1) Destructuring 襷ろ襦 レ語襯 螳 朱 覿襴 襦 List 豌 豬蠍 螳 襷ろ襦 螳 覲旧″ syntax 蟲譟一 (defmacro mix-and-match (p q) ;; not destruturing (let ((x1 (first p)) (y1 (second p)) (x2 (first q)) (y2 (second q))) `(list (,x1 ,y1) (,x1 ,y2) (,x2 ,y1) (,x2 ,y2)))) (defmacro mix-and-match ((x1 y1) (x2 y2)) ;; destruturing `(list (,x1 ,y1) (,x1 ,y2) (,x2 ,y1) (,x2 ,y2)))
  • 15. Advanced topics: Destructuring lambda lists - example DOVECTOR (defmacro dovector ((var vector-exp &optional result-form) &body body) `(do* ((vec-dov ,vector-exp) (len-dov (length vec-dov)) (i-dov 0 (+ i-dov 1)) (,var nil)) ((equal i-dov len-dov) ,result-form) (setf ,var (aref vec-dov i-dov)) ,@body)) > (dovector (x #(foo bar baz)) (format t "~&X is ~S" x)) X is FOO X is BAR X is BAZ NIL vec-dov, len-dov, i-dov 豐蠍郁 願鍵 讌 覲 覲 企 豢 襷蠍 伎 package system螻 gensyms襯 蟆 覦讌讌襷 豈 覯襯 覯 伎
  • 16. ?
  • 17. Advanced topics: Macros and lexical scoping 襦 incf 襷り鍵 螳 螳 蟆 狩蠍 symbol 覲 Read & Update螳 螳ロ伎 覲 螳 (defun faulty-incf (var) (set var (+ (symbol-value var) 1))) (setf a 7) > (faulty-incf a) 8 > (faulty-incf a) 9 > a 9 讌 覲 襷ろ襦襷 螳 (defun test-simple (turnip) (simple-incf turnip)) (defun test-faulty (turnip) (faulty-incf turnip)) > (test-simple 37) 38 > (test-faulty 37) Error: TURNIP unassigned variable. FAULTY-INCF 覿覈 lexical-context global-context願鍵 覓語 TEST-FAULTY 讌覲 TURNIP lexically 蟆 蠏殊 伎讌 .
  • 18. Advanced topics: Dynamic scoping Lexical scoping X覲 蠏狩蠍 伎 X螳 伎 body 襷 蠏手 DEFUN朱 伎 local 覲 覲 蠏 螳 讌 BAR lambda 朱 覲, BAR 覲 覲 蠏 螳 Dynamic scoping Dynamic variable Special variable企手 覿襴 覲 X螳 special襦 碁覃 るジ 讌 覲螳 螻 企 蠏殊 螳 DEFVAR 襷ろ襦襯 牛
  • 19. Advanced topics: Dynamic scoping - example (defvar birds) ;; bird襯 special襦 (setf fish (salmon tuna)) ;; fish lexical (setf birds (eagle vulture)) ;; birds special (defun ref-fish () ;; 谿語^ fish) (defun ref-birds () birds) (ref-fish) => (salmon tuna) ;; top level ろ (ref-birds) => (eagle vulture) (defun test-lexical (fish) (list fish (ref-fish))) > (test-lexical (guppy minnow)) ;; lexical 覲 谿語^ ((GUPPY MINNOW) (SALMON TUNA)) ;; ref-fish fish lexical scoping 磯 覲 谿語^ (defun test-dynamic (birds) (list birds (ref-birds))) ;; dynamic 覲 谿語^ > (test-dynamic (robin sparrow)) ;; ref-birds birds dynamic 覲 bird 谿語^ ((ROBIN SPARROW) (ROBIN SPARROW)) > (ref-birds) (EAGLE VULTURE) TEST-DYNAMIC body襦 れ 螳 襦 dynamic 覲 birds螳 蠍郁 TEST-DYNAMIC 蟾讌 覲 birds 願 谿語^ 蟆
  • 20. Advanced topics: defvar, defparameter, defconstant DEFVAR 豐蠍 螳 (defvar *total-glasses*) 覯 蠏 螳 碁覃 defvar襯 牛伎 覦蠖 > (defvar *total-glasses* 0 "Total glasses sold so far") > (defvar *total-glasses* 3 "Total glasses sold so far") > *total-glasses* 0 DEFPARAMETER 豐蠍 螳螻 蟷 伎 (defparameter abc 10) 碁 螳 defparameter襯 牛 覦蠖 > (defparameter *max-glasses* 500 "Maximum number of glasses we can make") > (defparameter *max-glasses* 300) > *max-glasses* 300 defvar defparameter 螳 覓碁 蟲譟一伎襷 defparameter 襦蠏碁 ろ 覲讌 螳 DEFCONSTANT 襷れ伎 覦讌 Lisp special valiableれ 蟯旧朱 *襦 語 讌襷 defconstant 碁ゼ Lisp PI 螳 伎 れ
  • 21. Advanced topics: Rebinding special variables Function 語 語襯 special 覲 企朱 讌 語 rebinding (defun print-in-base (*print-base* x) (format t "~&~D is written ~S in base ~D." x x *print-base*)) > (print-in-base 2 205) 205 is written 11001101 in base 2. NIL LET LET 牛 覦碁 (defvar *foo* 2) (defun bump-foo () (incf *foo*)) (defun rebind-boo () (let ((*foo* 100)) (incf *foo*) (bump-foo)))
  • 22. Ansi Lisp: Macro design example(1) ntimes 襷ろ襦 襷り鍵 ル 襷 覦覲 ろ 襷ろ襦 (ntimes count &body body) > (ntimes 10 (princ ".")) .......... NIL References ansi lisp http://onlisp.blogspot.com/2007/12/10-ansi-common-lisp.html
  • 23. Ansi Lisp: Macro design example(2) ntimes 襷ろ襦 (defmacro ntimes (n &rest body) `(do ((x 0 (+ x 1))) ((>= x ,n)) ,@body)) 覓語 覲 x襯 燕伎 蠍一ヾ 覲 x螳 る 豺 蟆郁骸 覦 覈視 (let ((x 10)) (ntimes 5 (setf x (+ x 1))) x) 10 X 10 麹螻 蠏 螳 5覯 讀螳 貅 讌襷 蟆郁骸 螳 蠏碁襦 10 襷ろ襦 (let ((x 10)) (do ((x 0 (+ x 1))) ((>= x 5)) (setf x (+ x 1))) ;; let朱 焔 x螳 do 覲 x螳 讀螳 x)
  • 24. Ansi Lisp: Macro design example(3) ntimes 襷ろ襦 do loop 覲襯 gensym 牛 襦蠏碁伎 企 覲手骸 螳讌 蟆 襷 (defmacro ntimes (n &rest body) (let ((g (gensym))) `(do ((,g 0 (+ ,g 1))) ((>= ,g ,n)) ,@body))) 覓語 覦覲 螳 do 覓語 body螳 螳 襷 n 襦蟆 螳 . 覈視 > (let ((v 10)) (ntimes (setf v (- v 1)) (princ "."))) ..... NIL 蟆郁骸 v 10 1 觝 9螳 豢 襷ろ襦 (let ((v 10)) (do ((#:g1 0 (+ #:g1 1))) ((>= #:g1 (setf v (- v 1)))) (princ ".")))
  • 25. Ansi Lisp: Macro design example(4) ntimes 襷ろ襦 (defmacro ntimes (n &rest body) (let ((g (gensym)) (h (gensym))) `(let ((,h ,n)) (do ((,g 0 (+ ,g 1))) ((>= ,g ,h)) ,@body)))) 覓語 願屋
  • 26.