cmput325: meta-programming fundamentalsrgreiner/c-325/...cmput325: meta-programming fundamentals b....
TRANSCRIPT
![Page 1: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/1.jpg)
CMPUT325: Meta-programming Fundamentals
B. Price and R. Greiner
29th September 2004
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 1
![Page 2: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/2.jpg)
Program is Data I
I A Lisp Program≈ an s-expr: (CAR '(1 2))
I Lisp interpreter executes the s-expr
I An s-expr is just a nested list structure
I Treated as a data structure, an s-expr can be traversed,composed or decomposed
I A program is just a nested list structure
I Programs can be traversed, composed or decomposed
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 2
![Page 3: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/3.jpg)
Program is Data I
I A Lisp Program≈ an s-expr: (CAR '(1 2))
I Lisp interpreter executes the s-expr
I An s-expr is just a nested list structure
I Treated as a data structure, an s-expr can be traversed,composed or decomposed
I A program is just a nested list structure
I Programs can be traversed, composed or decomposed
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 2
![Page 4: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/4.jpg)
Program is Data I
I A Lisp Program≈ an s-expr: (CAR '(1 2))
I Lisp interpreter executes the s-expr
I An s-expr is just a nested list structure
I Treated as a data structure, an s-expr can be traversed,composed or decomposed
I A program is just a nested list structure
I Programs can be traversed, composed or decomposed
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 2
![Page 5: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/5.jpg)
Program is Data I
I A Lisp Program≈ an s-expr: (CAR '(1 2))
I Lisp interpreter executes the s-expr
I An s-expr is just a nested list structure
I Treated as a data structure, an s-expr can be traversed,composed or decomposed
I A program is just a nested list structure
I Programs can be traversed, composed or decomposed
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 2
![Page 6: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/6.jpg)
Program is Data I
I A Lisp Program≈ an s-expr: (CAR '(1 2))
I Lisp interpreter executes the s-expr
I An s-expr is just a nested list structure
I Treated as a data structure, an s-expr can be traversed,composed or decomposed
I A program is just a nested list structure
I Programs can be traversed, composed or decomposed
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 2
![Page 7: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/7.jpg)
Program is Data I
I A Lisp Program≈ an s-expr: (CAR '(1 2))
I Lisp interpreter executes the s-expr
I An s-expr is just a nested list structure
I Treated as a data structure, an s-expr can be traversed,composed or decomposed
I A program is just a nested list structure
I Programs can be traversed, composed or decomposed
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 2
![Page 8: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/8.jpg)
Program is Data II
I Consider the program
(LAMBDA (fn) (funcall fn fn) )
I It uses fn as a program (function to be called)and as data (arguments for function)
I We can call this function on a λ
( (LAMBDA (fn) (funcall fn fn))'(LAMBA (X) (CAR x) ) )
I The λ argument is used as both program and data
≡ ( (LAMBDA (x) (CAR x)) '(LAMBDA (x) (CAR x)) )
→ LAMBDA
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 3
![Page 9: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/9.jpg)
Program is Data II
I Consider the program
(LAMBDA (fn) (funcall fn fn) )
I It uses fn as a program (function to be called)and as data (arguments for function)
I We can call this function on a λ
( (LAMBDA (fn) (funcall fn fn))'(LAMBA (X) (CAR x) ) )
I The λ argument is used as both program and data
≡ ( (LAMBDA (x) (CAR x)) '(LAMBDA (x) (CAR x)) )
→ LAMBDA
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 3
![Page 10: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/10.jpg)
Program is Data II
I Consider the program
(LAMBDA (fn) (funcall fn fn) )
I It uses fn as a program (function to be called)and as data (arguments for function)
I We can call this function on a λ
( (LAMBDA (fn) (funcall fn fn))'(LAMBA (X) (CAR x) ) )
I The λ argument is used as both program and data
≡ ( (LAMBDA (x) (CAR x)) '(LAMBDA (x) (CAR x)) )
→ LAMBDA
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 3
![Page 11: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/11.jpg)
Program is Data II
I Consider the program
(LAMBDA (fn) (funcall fn fn) )
I It uses fn as a program (function to be called)and as data (arguments for function)
I We can call this function on a λ
( (LAMBDA (fn) (funcall fn fn))'(LAMBA (X) (CAR x) ) )
I The λ argument is used as both program and data
≡ ( (LAMBDA (x) (CAR x)) '(LAMBDA (x) (CAR x)) )
→
LAMBDA
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 3
![Page 12: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/12.jpg)
Program is Data II
I Consider the program
(LAMBDA (fn) (funcall fn fn) )
I It uses fn as a program (function to be called)and as data (arguments for function)
I We can call this function on a λ
( (LAMBDA (fn) (funcall fn fn))'(LAMBA (X) (CAR x) ) )
I The λ argument is used as both program and data
≡ ( (LAMBDA (x) (CAR x)) '(LAMBDA (x) (CAR x)) )
→ LAMBDAB. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 3
![Page 13: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/13.jpg)
Other examples
( (LAMBDA (fn) (funcall fn fn)) 'ATOM )→
t( (LAMBDA (fn) (funcall fn fn)) CAR )→The function CAR (the variable) is undefined( (LAMBDA (fn) (funcall fn fn)) 'CAR )→ Error: CAR expects a list!( (LAMBDA (fn) (funcall fn fn)) '(LAMBDA (x) x) )→ (LAMBDA (x) x)( (LAMBDA (fn) (funcall fn fn)) '(LAMBDA (x) (x x)) )→ The function x is undefined( (LAMBDA (fn) (funcall fn fn)) '(LAMBDA (x) (funcall x x)) )→ ... waiting ... waiting ...
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 4
![Page 14: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/14.jpg)
Other examples
( (LAMBDA (fn) (funcall fn fn)) 'ATOM )→ t
( (LAMBDA (fn) (funcall fn fn)) CAR )→The function CAR (the variable) is undefined( (LAMBDA (fn) (funcall fn fn)) 'CAR )→ Error: CAR expects a list!( (LAMBDA (fn) (funcall fn fn)) '(LAMBDA (x) x) )→ (LAMBDA (x) x)( (LAMBDA (fn) (funcall fn fn)) '(LAMBDA (x) (x x)) )→ The function x is undefined( (LAMBDA (fn) (funcall fn fn)) '(LAMBDA (x) (funcall x x)) )→ ... waiting ... waiting ...
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 4
![Page 15: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/15.jpg)
Other examples
( (LAMBDA (fn) (funcall fn fn)) 'ATOM )→ t( (LAMBDA (fn) (funcall fn fn)) CAR )→
The function CAR (the variable) is undefined( (LAMBDA (fn) (funcall fn fn)) 'CAR )→ Error: CAR expects a list!( (LAMBDA (fn) (funcall fn fn)) '(LAMBDA (x) x) )→ (LAMBDA (x) x)( (LAMBDA (fn) (funcall fn fn)) '(LAMBDA (x) (x x)) )→ The function x is undefined( (LAMBDA (fn) (funcall fn fn)) '(LAMBDA (x) (funcall x x)) )→ ... waiting ... waiting ...
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 4
![Page 16: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/16.jpg)
Other examples
( (LAMBDA (fn) (funcall fn fn)) 'ATOM )→ t( (LAMBDA (fn) (funcall fn fn)) CAR )→The function CAR (the variable) is undefined
( (LAMBDA (fn) (funcall fn fn)) 'CAR )→ Error: CAR expects a list!( (LAMBDA (fn) (funcall fn fn)) '(LAMBDA (x) x) )→ (LAMBDA (x) x)( (LAMBDA (fn) (funcall fn fn)) '(LAMBDA (x) (x x)) )→ The function x is undefined( (LAMBDA (fn) (funcall fn fn)) '(LAMBDA (x) (funcall x x)) )→ ... waiting ... waiting ...
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 4
![Page 17: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/17.jpg)
Other examples
( (LAMBDA (fn) (funcall fn fn)) 'ATOM )→ t( (LAMBDA (fn) (funcall fn fn)) CAR )→The function CAR (the variable) is undefined( (LAMBDA (fn) (funcall fn fn)) 'CAR )→
Error: CAR expects a list!( (LAMBDA (fn) (funcall fn fn)) '(LAMBDA (x) x) )→ (LAMBDA (x) x)( (LAMBDA (fn) (funcall fn fn)) '(LAMBDA (x) (x x)) )→ The function x is undefined( (LAMBDA (fn) (funcall fn fn)) '(LAMBDA (x) (funcall x x)) )→ ... waiting ... waiting ...
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 4
![Page 18: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/18.jpg)
Other examples
( (LAMBDA (fn) (funcall fn fn)) 'ATOM )→ t( (LAMBDA (fn) (funcall fn fn)) CAR )→The function CAR (the variable) is undefined( (LAMBDA (fn) (funcall fn fn)) 'CAR )→ Error: CAR expects a list!
( (LAMBDA (fn) (funcall fn fn)) '(LAMBDA (x) x) )→ (LAMBDA (x) x)( (LAMBDA (fn) (funcall fn fn)) '(LAMBDA (x) (x x)) )→ The function x is undefined( (LAMBDA (fn) (funcall fn fn)) '(LAMBDA (x) (funcall x x)) )→ ... waiting ... waiting ...
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 4
![Page 19: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/19.jpg)
Other examples
( (LAMBDA (fn) (funcall fn fn)) 'ATOM )→ t( (LAMBDA (fn) (funcall fn fn)) CAR )→The function CAR (the variable) is undefined( (LAMBDA (fn) (funcall fn fn)) 'CAR )→ Error: CAR expects a list!( (LAMBDA (fn) (funcall fn fn)) '(LAMBDA (x) x) )→
(LAMBDA (x) x)( (LAMBDA (fn) (funcall fn fn)) '(LAMBDA (x) (x x)) )→ The function x is undefined( (LAMBDA (fn) (funcall fn fn)) '(LAMBDA (x) (funcall x x)) )→ ... waiting ... waiting ...
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 4
![Page 20: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/20.jpg)
Other examples
( (LAMBDA (fn) (funcall fn fn)) 'ATOM )→ t( (LAMBDA (fn) (funcall fn fn)) CAR )→The function CAR (the variable) is undefined( (LAMBDA (fn) (funcall fn fn)) 'CAR )→ Error: CAR expects a list!( (LAMBDA (fn) (funcall fn fn)) '(LAMBDA (x) x) )→ (LAMBDA (x) x)
( (LAMBDA (fn) (funcall fn fn)) '(LAMBDA (x) (x x)) )→ The function x is undefined( (LAMBDA (fn) (funcall fn fn)) '(LAMBDA (x) (funcall x x)) )→ ... waiting ... waiting ...
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 4
![Page 21: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/21.jpg)
Other examples
( (LAMBDA (fn) (funcall fn fn)) 'ATOM )→ t( (LAMBDA (fn) (funcall fn fn)) CAR )→The function CAR (the variable) is undefined( (LAMBDA (fn) (funcall fn fn)) 'CAR )→ Error: CAR expects a list!( (LAMBDA (fn) (funcall fn fn)) '(LAMBDA (x) x) )→ (LAMBDA (x) x)( (LAMBDA (fn) (funcall fn fn)) '(LAMBDA (x) (x x)) )→
The function x is undefined( (LAMBDA (fn) (funcall fn fn)) '(LAMBDA (x) (funcall x x)) )→ ... waiting ... waiting ...
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 4
![Page 22: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/22.jpg)
Other examples
( (LAMBDA (fn) (funcall fn fn)) 'ATOM )→ t( (LAMBDA (fn) (funcall fn fn)) CAR )→The function CAR (the variable) is undefined( (LAMBDA (fn) (funcall fn fn)) 'CAR )→ Error: CAR expects a list!( (LAMBDA (fn) (funcall fn fn)) '(LAMBDA (x) x) )→ (LAMBDA (x) x)( (LAMBDA (fn) (funcall fn fn)) '(LAMBDA (x) (x x)) )→ The function x is undefined
( (LAMBDA (fn) (funcall fn fn)) '(LAMBDA (x) (funcall x x)) )→ ... waiting ... waiting ...
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 4
![Page 23: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/23.jpg)
Other examples
( (LAMBDA (fn) (funcall fn fn)) 'ATOM )→ t( (LAMBDA (fn) (funcall fn fn)) CAR )→The function CAR (the variable) is undefined( (LAMBDA (fn) (funcall fn fn)) 'CAR )→ Error: CAR expects a list!( (LAMBDA (fn) (funcall fn fn)) '(LAMBDA (x) x) )→ (LAMBDA (x) x)( (LAMBDA (fn) (funcall fn fn)) '(LAMBDA (x) (x x)) )→ The function x is undefined( (LAMBDA (fn) (funcall fn fn)) '(LAMBDA (x) (funcall x x)) )→
... waiting ... waiting ...
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 4
![Page 24: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/24.jpg)
Other examples
( (LAMBDA (fn) (funcall fn fn)) 'ATOM )→ t( (LAMBDA (fn) (funcall fn fn)) CAR )→The function CAR (the variable) is undefined( (LAMBDA (fn) (funcall fn fn)) 'CAR )→ Error: CAR expects a list!( (LAMBDA (fn) (funcall fn fn)) '(LAMBDA (x) x) )→ (LAMBDA (x) x)( (LAMBDA (fn) (funcall fn fn)) '(LAMBDA (x) (x x)) )→ The function x is undefined( (LAMBDA (fn) (funcall fn fn)) '(LAMBDA (x) (funcall x x)) )→ ... waiting ... waiting ...
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 4
![Page 25: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/25.jpg)
Modifying Code I
(setf (symbol-function 'foo) '(LAMBDA (x) (CADR x)) )
I Why do I need symbol-function?There is a separate table for the data values and functionvalues of symbols.
(setf foo 2)foo → 2(symbol-function 'foo)→ (LAMBDA (x) (CADR x))(foo '(A B C)) → B(CONS (CAR (symbol-function 'foo)) '((u y z) y))→(LAMBDA (u y z) y)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 5
![Page 26: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/26.jpg)
Modifying Code I
(setf (symbol-function 'foo) '(LAMBDA (x) (CADR x)) )
I Why do I need symbol-function?
There is a separate table for the data values and functionvalues of symbols.
(setf foo 2)foo → 2(symbol-function 'foo)→ (LAMBDA (x) (CADR x))(foo '(A B C)) → B(CONS (CAR (symbol-function 'foo)) '((u y z) y))→(LAMBDA (u y z) y)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 5
![Page 27: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/27.jpg)
Modifying Code I
(setf (symbol-function 'foo) '(LAMBDA (x) (CADR x)) )
I Why do I need symbol-function?There is a separate table for the data values and functionvalues of symbols.
(setf foo 2)foo → 2(symbol-function 'foo)→ (LAMBDA (x) (CADR x))(foo '(A B C)) → B(CONS (CAR (symbol-function 'foo)) '((u y z) y))→(LAMBDA (u y z) y)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 5
![Page 28: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/28.jpg)
Modifying Code I
(setf (symbol-function 'foo) '(LAMBDA (x) (CADR x)) )
I Why do I need symbol-function?There is a separate table for the data values and functionvalues of symbols.
(setf foo 2)foo →
2(symbol-function 'foo)→ (LAMBDA (x) (CADR x))(foo '(A B C)) → B(CONS (CAR (symbol-function 'foo)) '((u y z) y))→(LAMBDA (u y z) y)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 5
![Page 29: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/29.jpg)
Modifying Code I
(setf (symbol-function 'foo) '(LAMBDA (x) (CADR x)) )
I Why do I need symbol-function?There is a separate table for the data values and functionvalues of symbols.
(setf foo 2)foo → 2
(symbol-function 'foo)→ (LAMBDA (x) (CADR x))(foo '(A B C)) → B(CONS (CAR (symbol-function 'foo)) '((u y z) y))→(LAMBDA (u y z) y)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 5
![Page 30: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/30.jpg)
Modifying Code I
(setf (symbol-function 'foo) '(LAMBDA (x) (CADR x)) )
I Why do I need symbol-function?There is a separate table for the data values and functionvalues of symbols.
(setf foo 2)foo → 2(symbol-function 'foo)→
(LAMBDA (x) (CADR x))(foo '(A B C)) → B(CONS (CAR (symbol-function 'foo)) '((u y z) y))→(LAMBDA (u y z) y)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 5
![Page 31: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/31.jpg)
Modifying Code I
(setf (symbol-function 'foo) '(LAMBDA (x) (CADR x)) )
I Why do I need symbol-function?There is a separate table for the data values and functionvalues of symbols.
(setf foo 2)foo → 2(symbol-function 'foo)→ (LAMBDA (x) (CADR x))
(foo '(A B C)) → B(CONS (CAR (symbol-function 'foo)) '((u y z) y))→(LAMBDA (u y z) y)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 5
![Page 32: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/32.jpg)
Modifying Code I
(setf (symbol-function 'foo) '(LAMBDA (x) (CADR x)) )
I Why do I need symbol-function?There is a separate table for the data values and functionvalues of symbols.
(setf foo 2)foo → 2(symbol-function 'foo)→ (LAMBDA (x) (CADR x))(foo '(A B C)) →
B(CONS (CAR (symbol-function 'foo)) '((u y z) y))→(LAMBDA (u y z) y)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 5
![Page 33: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/33.jpg)
Modifying Code I
(setf (symbol-function 'foo) '(LAMBDA (x) (CADR x)) )
I Why do I need symbol-function?There is a separate table for the data values and functionvalues of symbols.
(setf foo 2)foo → 2(symbol-function 'foo)→ (LAMBDA (x) (CADR x))(foo '(A B C)) → B
(CONS (CAR (symbol-function 'foo)) '((u y z) y))→(LAMBDA (u y z) y)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 5
![Page 34: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/34.jpg)
Modifying Code I
(setf (symbol-function 'foo) '(LAMBDA (x) (CADR x)) )
I Why do I need symbol-function?There is a separate table for the data values and functionvalues of symbols.
(setf foo 2)foo → 2(symbol-function 'foo)→ (LAMBDA (x) (CADR x))(foo '(A B C)) → B(CONS (CAR (symbol-function 'foo)) '((u y z) y))→
(LAMBDA (u y z) y)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 5
![Page 35: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/35.jpg)
Modifying Code I
(setf (symbol-function 'foo) '(LAMBDA (x) (CADR x)) )
I Why do I need symbol-function?There is a separate table for the data values and functionvalues of symbols.
(setf foo 2)foo → 2(symbol-function 'foo)→ (LAMBDA (x) (CADR x))(foo '(A B C)) → B(CONS (CAR (symbol-function 'foo)) '((u y z) y))→(LAMBDA (u y z) y)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 5
![Page 36: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/36.jpg)
Modifying Code II
(setf (symbol-function 'bar)(CONS (CAR (symbol-function 'foo)) '((u y z) y)))
→
(LAMBDA (u y z) y)(bar 4 (+ 2 3) '(t Q)) → 5(setf (symbol-function 'N-args)
'(LAMBDA (x) (LENGTH (CADR (symbol-function x)))))→(LAMBDA (x) (LENGTH (SECOND (symbol-function x))))(N-args 'foo)→ 1(N-args 'bar)→ 3(N-args 'N-args)→ 1
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 6
![Page 37: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/37.jpg)
Modifying Code II
(setf (symbol-function 'bar)(CONS (CAR (symbol-function 'foo)) '((u y z) y)))
→ (LAMBDA (u y z) y)
(bar 4 (+ 2 3) '(t Q)) → 5(setf (symbol-function 'N-args)
'(LAMBDA (x) (LENGTH (CADR (symbol-function x)))))→(LAMBDA (x) (LENGTH (SECOND (symbol-function x))))(N-args 'foo)→ 1(N-args 'bar)→ 3(N-args 'N-args)→ 1
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 6
![Page 38: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/38.jpg)
Modifying Code II
(setf (symbol-function 'bar)(CONS (CAR (symbol-function 'foo)) '((u y z) y)))
→ (LAMBDA (u y z) y)(bar 4 (+ 2 3) '(t Q)) →
5(setf (symbol-function 'N-args)
'(LAMBDA (x) (LENGTH (CADR (symbol-function x)))))→(LAMBDA (x) (LENGTH (SECOND (symbol-function x))))(N-args 'foo)→ 1(N-args 'bar)→ 3(N-args 'N-args)→ 1
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 6
![Page 39: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/39.jpg)
Modifying Code II
(setf (symbol-function 'bar)(CONS (CAR (symbol-function 'foo)) '((u y z) y)))
→ (LAMBDA (u y z) y)(bar 4 (+ 2 3) '(t Q)) → 5
(setf (symbol-function 'N-args)'(LAMBDA (x) (LENGTH (CADR (symbol-function x)))))
→(LAMBDA (x) (LENGTH (SECOND (symbol-function x))))(N-args 'foo)→ 1(N-args 'bar)→ 3(N-args 'N-args)→ 1
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 6
![Page 40: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/40.jpg)
Modifying Code II
(setf (symbol-function 'bar)(CONS (CAR (symbol-function 'foo)) '((u y z) y)))
→ (LAMBDA (u y z) y)(bar 4 (+ 2 3) '(t Q)) → 5(setf (symbol-function 'N-args)
'(LAMBDA (x) (LENGTH (CADR (symbol-function x)))))→
(LAMBDA (x) (LENGTH (SECOND (symbol-function x))))(N-args 'foo)→ 1(N-args 'bar)→ 3(N-args 'N-args)→ 1
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 6
![Page 41: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/41.jpg)
Modifying Code II
(setf (symbol-function 'bar)(CONS (CAR (symbol-function 'foo)) '((u y z) y)))
→ (LAMBDA (u y z) y)(bar 4 (+ 2 3) '(t Q)) → 5(setf (symbol-function 'N-args)
'(LAMBDA (x) (LENGTH (CADR (symbol-function x)))))→(LAMBDA (x) (LENGTH (SECOND (symbol-function x))))
(N-args 'foo)→ 1(N-args 'bar)→ 3(N-args 'N-args)→ 1
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 6
![Page 42: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/42.jpg)
Modifying Code II
(setf (symbol-function 'bar)(CONS (CAR (symbol-function 'foo)) '((u y z) y)))
→ (LAMBDA (u y z) y)(bar 4 (+ 2 3) '(t Q)) → 5(setf (symbol-function 'N-args)
'(LAMBDA (x) (LENGTH (CADR (symbol-function x)))))→(LAMBDA (x) (LENGTH (SECOND (symbol-function x))))(N-args 'foo)→
1(N-args 'bar)→ 3(N-args 'N-args)→ 1
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 6
![Page 43: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/43.jpg)
Modifying Code II
(setf (symbol-function 'bar)(CONS (CAR (symbol-function 'foo)) '((u y z) y)))
→ (LAMBDA (u y z) y)(bar 4 (+ 2 3) '(t Q)) → 5(setf (symbol-function 'N-args)
'(LAMBDA (x) (LENGTH (CADR (symbol-function x)))))→(LAMBDA (x) (LENGTH (SECOND (symbol-function x))))(N-args 'foo)→ 1
(N-args 'bar)→ 3(N-args 'N-args)→ 1
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 6
![Page 44: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/44.jpg)
Modifying Code II
(setf (symbol-function 'bar)(CONS (CAR (symbol-function 'foo)) '((u y z) y)))
→ (LAMBDA (u y z) y)(bar 4 (+ 2 3) '(t Q)) → 5(setf (symbol-function 'N-args)
'(LAMBDA (x) (LENGTH (CADR (symbol-function x)))))→(LAMBDA (x) (LENGTH (SECOND (symbol-function x))))(N-args 'foo)→ 1(N-args 'bar)→
3(N-args 'N-args)→ 1
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 6
![Page 45: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/45.jpg)
Modifying Code II
(setf (symbol-function 'bar)(CONS (CAR (symbol-function 'foo)) '((u y z) y)))
→ (LAMBDA (u y z) y)(bar 4 (+ 2 3) '(t Q)) → 5(setf (symbol-function 'N-args)
'(LAMBDA (x) (LENGTH (CADR (symbol-function x)))))→(LAMBDA (x) (LENGTH (SECOND (symbol-function x))))(N-args 'foo)→ 1(N-args 'bar)→ 3
(N-args 'N-args)→ 1
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 6
![Page 46: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/46.jpg)
Modifying Code II
(setf (symbol-function 'bar)(CONS (CAR (symbol-function 'foo)) '((u y z) y)))
→ (LAMBDA (u y z) y)(bar 4 (+ 2 3) '(t Q)) → 5(setf (symbol-function 'N-args)
'(LAMBDA (x) (LENGTH (CADR (symbol-function x)))))→(LAMBDA (x) (LENGTH (SECOND (symbol-function x))))(N-args 'foo)→ 1(N-args 'bar)→ 3(N-args 'N-args)→
1
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 6
![Page 47: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/47.jpg)
Modifying Code II
(setf (symbol-function 'bar)(CONS (CAR (symbol-function 'foo)) '((u y z) y)))
→ (LAMBDA (u y z) y)(bar 4 (+ 2 3) '(t Q)) → 5(setf (symbol-function 'N-args)
'(LAMBDA (x) (LENGTH (CADR (symbol-function x)))))→(LAMBDA (x) (LENGTH (SECOND (symbol-function x))))(N-args 'foo)→ 1(N-args 'bar)→ 3(N-args 'N-args)→ 1
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 6
![Page 48: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/48.jpg)
Compiler vs Interpreter
COMPILER translates Source Program into Executable ObjectCode
Steps in Compiler-Based System:
1. read program
2. check syntax & type agreement
3. compile3.1 produce "object code"3.2 discard "source" program
4. run object code
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 7
![Page 49: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/49.jpg)
Compiler vs Interpreter
COMPILER translates Source Program into Executable ObjectCodeSteps in Compiler-Based System:
1. read program
2. check syntax & type agreement
3. compile3.1 produce "object code"3.2 discard "source" program
4. run object code
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 7
![Page 50: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/50.jpg)
Compiler vs Interpreter
COMPILER translates Source Program into Executable ObjectCodeSteps in Compiler-Based System:
1. read program
2. check syntax & type agreement
3. compile3.1 produce "object code"3.2 discard "source" program
4. run object code
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 7
![Page 51: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/51.jpg)
Compiler vs Interpreter
COMPILER translates Source Program into Executable ObjectCodeSteps in Compiler-Based System:
1. read program
2. check syntax & type agreement
3. compile3.1 produce "object code"3.2 discard "source" program
4. run object code
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 7
![Page 52: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/52.jpg)
Compiler vs Interpreter
COMPILER translates Source Program into Executable ObjectCodeSteps in Compiler-Based System:
1. read program
2. check syntax & type agreement
3. compile3.1 produce "object code"3.2 discard "source" program
4. run object code
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 7
![Page 53: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/53.jpg)
Compiler vs Interpreter
COMPILER translates Source Program into Executable ObjectCodeSteps in Compiler-Based System:
1. read program
2. check syntax & type agreement
3. compile3.1 produce "object code"3.2 discard "source" program
4. run object code
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 7
![Page 54: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/54.jpg)
Compiler vs Interpreter
INTERPRETER directly executes (Source) Program
Steps:
1. read next form1.1 evaluate (aka "execute") it1.2 print value
2. Notes:2.1 form may be a program (s-expr)2.2 only run-time checks performe
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 8
![Page 55: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/55.jpg)
Compiler vs Interpreter
INTERPRETER directly executes (Source) ProgramSteps:
1. read next form1.1 evaluate (aka "execute") it1.2 print value
2. Notes:2.1 form may be a program (s-expr)2.2 only run-time checks performe
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 8
![Page 56: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/56.jpg)
Compiler vs Interpreter
INTERPRETER directly executes (Source) ProgramSteps:
1. read next form1.1 evaluate (aka "execute") it1.2 print value
2. Notes:2.1 form may be a program (s-expr)2.2 only run-time checks performe
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 8
![Page 57: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/57.jpg)
Compiler vs Interpreter
INTERPRETER directly executes (Source) ProgramSteps:
1. read next form1.1 evaluate (aka "execute") it1.2 print value
2. Notes:2.1 form may be a program (s-expr)2.2 only run-time checks performe
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 8
![Page 58: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/58.jpg)
Lisp System
I Many Lisps have compilers - both byte-code and native
I Most Lisps include INTERPRETERs.READ-EVAL-PRINT Loop
I Some Lisp's (s-lisp) call compiler after each read so code isalways compiled
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 9
![Page 59: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/59.jpg)
Lisp System
I Many Lisps have compilers - both byte-code and native
I Most Lisps include INTERPRETERs.READ-EVAL-PRINT Loop
I Some Lisp's (s-lisp) call compiler after each read so code isalways compiled
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 9
![Page 60: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/60.jpg)
Lisp System
I Many Lisps have compilers - both byte-code and native
I Most Lisps include INTERPRETERs.READ-EVAL-PRINT Loop
I Some Lisp's (s-lisp) call compiler after each read so code isalways compiled
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 9
![Page 61: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/61.jpg)
LISP Interpretation: EVAL
I Interpretation is based on Evaluationwhich maps S-expr into S-expr
(CONS (CAR '(A B)) '(C D)) → (A C D)
I Can write a Lisp Function to do it!EVAL of 〈form〉 is 〈form〉's value.
I Use Subset of Lisp:
〈form〉::= (QUOTE 〈s − expr〉)| (CAR 〈form〉)| (CDR 〈form〉)| (CONS 〈form〉 〈form〉 )| t | nil
(EVAL '(CONS t nil)) → (t)(EVAL '(CONS (CAR '(A B)) '(C D))) → (A C D)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 10
![Page 62: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/62.jpg)
LISP Interpretation: EVAL
I Interpretation is based on Evaluationwhich maps S-expr into S-expr
(CONS (CAR '(A B)) '(C D)) → (A C D)
I Can write a Lisp Function to do it!EVAL of 〈form〉 is 〈form〉's value.
I Use Subset of Lisp:
〈form〉::= (QUOTE 〈s − expr〉)| (CAR 〈form〉)| (CDR 〈form〉)| (CONS 〈form〉 〈form〉 )| t | nil
(EVAL '(CONS t nil)) → (t)(EVAL '(CONS (CAR '(A B)) '(C D))) → (A C D)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 10
![Page 63: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/63.jpg)
LISP Interpretation: EVAL
I Interpretation is based on Evaluationwhich maps S-expr into S-expr
(CONS (CAR '(A B)) '(C D)) → (A C D)
I Can write a Lisp Function to do it!EVAL of 〈form〉 is 〈form〉's value.
I Use Subset of Lisp:
〈form〉::= (QUOTE 〈s − expr〉)| (CAR 〈form〉)| (CDR 〈form〉)| (CONS 〈form〉 〈form〉 )| t | nil
(EVAL '(CONS t nil)) → (t)(EVAL '(CONS (CAR '(A B)) '(C D))) → (A C D)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 10
![Page 64: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/64.jpg)
LISP Interpretation: EVAL
I Interpretation is based on Evaluationwhich maps S-expr into S-expr
(CONS (CAR '(A B)) '(C D)) → (A C D)
I Can write a Lisp Function to do it!EVAL of 〈form〉 is 〈form〉's value.
I Use Subset of Lisp:
〈form〉::= (QUOTE 〈s − expr〉)| (CAR 〈form〉)| (CDR 〈form〉)| (CONS 〈form〉 〈form〉 )| t | nil
(EVAL '(CONS t nil)) →
(t)(EVAL '(CONS (CAR '(A B)) '(C D))) → (A C D)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 10
![Page 65: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/65.jpg)
LISP Interpretation: EVAL
I Interpretation is based on Evaluationwhich maps S-expr into S-expr
(CONS (CAR '(A B)) '(C D)) → (A C D)
I Can write a Lisp Function to do it!EVAL of 〈form〉 is 〈form〉's value.
I Use Subset of Lisp:
〈form〉::= (QUOTE 〈s − expr〉)| (CAR 〈form〉)| (CDR 〈form〉)| (CONS 〈form〉 〈form〉 )| t | nil
(EVAL '(CONS t nil)) → (t)
(EVAL '(CONS (CAR '(A B)) '(C D))) → (A C D)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 10
![Page 66: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/66.jpg)
LISP Interpretation: EVAL
I Interpretation is based on Evaluationwhich maps S-expr into S-expr
(CONS (CAR '(A B)) '(C D)) → (A C D)
I Can write a Lisp Function to do it!EVAL of 〈form〉 is 〈form〉's value.
I Use Subset of Lisp:
〈form〉::= (QUOTE 〈s − expr〉)| (CAR 〈form〉)| (CDR 〈form〉)| (CONS 〈form〉 〈form〉 )| t | nil
(EVAL '(CONS t nil)) → (t)(EVAL '(CONS (CAR '(A B)) '(C D))) →
(A C D)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 10
![Page 67: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/67.jpg)
LISP Interpretation: EVAL
I Interpretation is based on Evaluationwhich maps S-expr into S-expr
(CONS (CAR '(A B)) '(C D)) → (A C D)
I Can write a Lisp Function to do it!EVAL of 〈form〉 is 〈form〉's value.
I Use Subset of Lisp:
〈form〉::= (QUOTE 〈s − expr〉)| (CAR 〈form〉)| (CDR 〈form〉)| (CONS 〈form〉 〈form〉 )| t | nil
(EVAL '(CONS t nil)) → (t)(EVAL '(CONS (CAR '(A B)) '(C D))) → (A C D)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 10
![Page 68: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/68.jpg)
EVAL wrt Variables
I Problem: What does x evaluate to in:
(EVAL '(CONS x '(B C))) ?
I Solution: Specify the CONTEXT of the evaluation with anAssocList
( (x foo) (y (t nil)) (z nil) )
I AssocList is a mini data base
I EVAL takes 2 args: form + context
(EVAL '(CONS x '(B C))'( (x t ) ( y (t nil)) (z nil))
→ (t B C)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 11
![Page 69: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/69.jpg)
EVAL wrt Variables
I Problem: What does x evaluate to in:
(EVAL '(CONS x '(B C))) ?
I Solution: Specify the CONTEXT of the evaluation with anAssocList
( (x foo) (y (t nil)) (z nil) )
I AssocList is a mini data base
I EVAL takes 2 args: form + context
(EVAL '(CONS x '(B C))'( (x t ) ( y (t nil)) (z nil))
→ (t B C)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 11
![Page 70: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/70.jpg)
EVAL wrt Variables
I Problem: What does x evaluate to in:
(EVAL '(CONS x '(B C))) ?
I Solution: Specify the CONTEXT of the evaluation with anAssocList
( (x foo) (y (t nil)) (z nil) )
I AssocList is a mini data base
I EVAL takes 2 args: form + context
(EVAL '(CONS x '(B C))'( (x t ) ( y (t nil)) (z nil))
→ (t B C)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 11
![Page 71: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/71.jpg)
EVAL wrt Variables
I Problem: What does x evaluate to in:
(EVAL '(CONS x '(B C))) ?
I Solution: Specify the CONTEXT of the evaluation with anAssocList
( (x foo) (y (t nil)) (z nil) )
I AssocList is a mini data base
I EVAL takes 2 args: form + context
(EVAL '(CONS x '(B C))'( (x t ) ( y (t nil)) (z nil))
→
(t B C)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 11
![Page 72: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/72.jpg)
EVAL wrt Variables
I Problem: What does x evaluate to in:
(EVAL '(CONS x '(B C))) ?
I Solution: Specify the CONTEXT of the evaluation with anAssocList
( (x foo) (y (t nil)) (z nil) )
I AssocList is a mini data base
I EVAL takes 2 args: form + context
(EVAL '(CONS x '(B C))'( (x t ) ( y (t nil)) (z nil))
→ (t B C)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 11
![Page 73: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/73.jpg)
EVAL in General
I EVAL form + context ; s-expr(Common Lisp EVAL does not accept a context argument)
e ⇔ (eval 'e nil)EVAL of e (with nil context) is s-expr
I EVAL is a function;Can use like any other function!
I Can take only 1 argas if context = nil
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 12
![Page 74: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/74.jpg)
EVAL in General
I EVAL form + context ; s-expr(Common Lisp EVAL does not accept a context argument)
e ⇔ (eval 'e nil)EVAL of e (with nil context) is s-expr
I EVAL is a function;Can use like any other function!
I Can take only 1 argas if context = nil
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 12
![Page 75: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/75.jpg)
EVAL in General
I EVAL form + context ; s-expr(Common Lisp EVAL does not accept a context argument)
e ⇔ (eval 'e nil)EVAL of e (with nil context) is s-expr
I EVAL is a function;Can use like any other function!
I Can take only 1 argas if context = nil
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 12
![Page 76: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/76.jpg)
Examples of EVAL Ia
'(CONS 'a '(b c))
→ (CONS 'a '(b c))
(EVAL '(CONS 'a '(b c))) → (a b c)
(setq x '(list '+ 3 4)) → (list '+ 3 4)
'x → x
(eval 'x) → (list '+ 3 4)
x → (list '+ 3 4)
(eval (eval 'x))→ (+ 3 4)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 13
![Page 77: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/77.jpg)
Examples of EVAL Ia
'(CONS 'a '(b c)) → (CONS 'a '(b c))
(EVAL '(CONS 'a '(b c))) → (a b c)
(setq x '(list '+ 3 4)) → (list '+ 3 4)
'x → x
(eval 'x) → (list '+ 3 4)
x → (list '+ 3 4)
(eval (eval 'x))→ (+ 3 4)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 13
![Page 78: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/78.jpg)
Examples of EVAL Ia
'(CONS 'a '(b c)) → (CONS 'a '(b c))
(EVAL '(CONS 'a '(b c)))
→ (a b c)
(setq x '(list '+ 3 4)) → (list '+ 3 4)
'x → x
(eval 'x) → (list '+ 3 4)
x → (list '+ 3 4)
(eval (eval 'x))→ (+ 3 4)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 13
![Page 79: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/79.jpg)
Examples of EVAL Ia
'(CONS 'a '(b c)) → (CONS 'a '(b c))
(EVAL '(CONS 'a '(b c))) → (a b c)
(setq x '(list '+ 3 4)) → (list '+ 3 4)
'x → x
(eval 'x) → (list '+ 3 4)
x → (list '+ 3 4)
(eval (eval 'x))→ (+ 3 4)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 13
![Page 80: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/80.jpg)
Examples of EVAL Ia
'(CONS 'a '(b c)) → (CONS 'a '(b c))
(EVAL '(CONS 'a '(b c))) → (a b c)
(setq x '(list '+ 3 4))
→ (list '+ 3 4)
'x → x
(eval 'x) → (list '+ 3 4)
x → (list '+ 3 4)
(eval (eval 'x))→ (+ 3 4)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 13
![Page 81: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/81.jpg)
Examples of EVAL Ia
'(CONS 'a '(b c)) → (CONS 'a '(b c))
(EVAL '(CONS 'a '(b c))) → (a b c)
(setq x '(list '+ 3 4)) → (list '+ 3 4)
'x → x
(eval 'x) → (list '+ 3 4)
x → (list '+ 3 4)
(eval (eval 'x))→ (+ 3 4)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 13
![Page 82: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/82.jpg)
Examples of EVAL Ia
'(CONS 'a '(b c)) → (CONS 'a '(b c))
(EVAL '(CONS 'a '(b c))) → (a b c)
(setq x '(list '+ 3 4)) → (list '+ 3 4)
'x
→ x
(eval 'x) → (list '+ 3 4)
x → (list '+ 3 4)
(eval (eval 'x))→ (+ 3 4)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 13
![Page 83: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/83.jpg)
Examples of EVAL Ia
'(CONS 'a '(b c)) → (CONS 'a '(b c))
(EVAL '(CONS 'a '(b c))) → (a b c)
(setq x '(list '+ 3 4)) → (list '+ 3 4)
'x → x
(eval 'x) → (list '+ 3 4)
x → (list '+ 3 4)
(eval (eval 'x))→ (+ 3 4)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 13
![Page 84: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/84.jpg)
Examples of EVAL Ia
'(CONS 'a '(b c)) → (CONS 'a '(b c))
(EVAL '(CONS 'a '(b c))) → (a b c)
(setq x '(list '+ 3 4)) → (list '+ 3 4)
'x → x
(eval 'x)
→ (list '+ 3 4)
x → (list '+ 3 4)
(eval (eval 'x))→ (+ 3 4)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 13
![Page 85: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/85.jpg)
Examples of EVAL Ia
'(CONS 'a '(b c)) → (CONS 'a '(b c))
(EVAL '(CONS 'a '(b c))) → (a b c)
(setq x '(list '+ 3 4)) → (list '+ 3 4)
'x → x
(eval 'x) → (list '+ 3 4)
x → (list '+ 3 4)
(eval (eval 'x))→ (+ 3 4)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 13
![Page 86: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/86.jpg)
Examples of EVAL Ia
'(CONS 'a '(b c)) → (CONS 'a '(b c))
(EVAL '(CONS 'a '(b c))) → (a b c)
(setq x '(list '+ 3 4)) → (list '+ 3 4)
'x → x
(eval 'x) → (list '+ 3 4)
x
→ (list '+ 3 4)
(eval (eval 'x))→ (+ 3 4)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 13
![Page 87: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/87.jpg)
Examples of EVAL Ia
'(CONS 'a '(b c)) → (CONS 'a '(b c))
(EVAL '(CONS 'a '(b c))) → (a b c)
(setq x '(list '+ 3 4)) → (list '+ 3 4)
'x → x
(eval 'x) → (list '+ 3 4)
x → (list '+ 3 4)
(eval (eval 'x))→ (+ 3 4)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 13
![Page 88: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/88.jpg)
Examples of EVAL Ia
'(CONS 'a '(b c)) → (CONS 'a '(b c))
(EVAL '(CONS 'a '(b c))) → (a b c)
(setq x '(list '+ 3 4)) → (list '+ 3 4)
'x → x
(eval 'x) → (list '+ 3 4)
x → (list '+ 3 4)
(eval (eval 'x))
→ (+ 3 4)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 13
![Page 89: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/89.jpg)
Examples of EVAL Ia
'(CONS 'a '(b c)) → (CONS 'a '(b c))
(EVAL '(CONS 'a '(b c))) → (a b c)
(setq x '(list '+ 3 4)) → (list '+ 3 4)
'x → x
(eval 'x) → (list '+ 3 4)
x → (list '+ 3 4)
(eval (eval 'x))→ (+ 3 4)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 13
![Page 90: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/90.jpg)
Examples of EVAL Ib
(eval (eval x))
→7
(eval '(eval x)) →(+ 3 4)
(setq y 'x) → x
(eval 'y) → x
(eval '(QUOTE y)) → y
(eval y) →(list '+ 3 4)
(eval (eval y)) →(+ 3 4)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 14
![Page 91: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/91.jpg)
Examples of EVAL Ib
(eval (eval x)) →7
(eval '(eval x)) →(+ 3 4)
(setq y 'x) → x
(eval 'y) → x
(eval '(QUOTE y)) → y
(eval y) →(list '+ 3 4)
(eval (eval y)) →(+ 3 4)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 14
![Page 92: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/92.jpg)
Examples of EVAL Ib
(eval (eval x)) →7
(eval '(eval x))
→(+ 3 4)
(setq y 'x) → x
(eval 'y) → x
(eval '(QUOTE y)) → y
(eval y) →(list '+ 3 4)
(eval (eval y)) →(+ 3 4)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 14
![Page 93: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/93.jpg)
Examples of EVAL Ib
(eval (eval x)) →7
(eval '(eval x)) →(+ 3 4)
(setq y 'x) → x
(eval 'y) → x
(eval '(QUOTE y)) → y
(eval y) →(list '+ 3 4)
(eval (eval y)) →(+ 3 4)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 14
![Page 94: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/94.jpg)
Examples of EVAL Ib
(eval (eval x)) →7
(eval '(eval x)) →(+ 3 4)
(setq y 'x)
→ x
(eval 'y) → x
(eval '(QUOTE y)) → y
(eval y) →(list '+ 3 4)
(eval (eval y)) →(+ 3 4)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 14
![Page 95: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/95.jpg)
Examples of EVAL Ib
(eval (eval x)) →7
(eval '(eval x)) →(+ 3 4)
(setq y 'x) → x
(eval 'y) → x
(eval '(QUOTE y)) → y
(eval y) →(list '+ 3 4)
(eval (eval y)) →(+ 3 4)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 14
![Page 96: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/96.jpg)
Examples of EVAL Ib
(eval (eval x)) →7
(eval '(eval x)) →(+ 3 4)
(setq y 'x) → x
(eval 'y)
→ x
(eval '(QUOTE y)) → y
(eval y) →(list '+ 3 4)
(eval (eval y)) →(+ 3 4)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 14
![Page 97: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/97.jpg)
Examples of EVAL Ib
(eval (eval x)) →7
(eval '(eval x)) →(+ 3 4)
(setq y 'x) → x
(eval 'y) → x
(eval '(QUOTE y)) → y
(eval y) →(list '+ 3 4)
(eval (eval y)) →(+ 3 4)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 14
![Page 98: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/98.jpg)
Examples of EVAL Ib
(eval (eval x)) →7
(eval '(eval x)) →(+ 3 4)
(setq y 'x) → x
(eval 'y) → x
(eval '(QUOTE y))
→ y
(eval y) →(list '+ 3 4)
(eval (eval y)) →(+ 3 4)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 14
![Page 99: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/99.jpg)
Examples of EVAL Ib
(eval (eval x)) →7
(eval '(eval x)) →(+ 3 4)
(setq y 'x) → x
(eval 'y) → x
(eval '(QUOTE y)) → y
(eval y) →(list '+ 3 4)
(eval (eval y)) →(+ 3 4)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 14
![Page 100: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/100.jpg)
Examples of EVAL Ib
(eval (eval x)) →7
(eval '(eval x)) →(+ 3 4)
(setq y 'x) → x
(eval 'y) → x
(eval '(QUOTE y)) → y
(eval y)
→(list '+ 3 4)
(eval (eval y)) →(+ 3 4)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 14
![Page 101: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/101.jpg)
Examples of EVAL Ib
(eval (eval x)) →7
(eval '(eval x)) →(+ 3 4)
(setq y 'x) → x
(eval 'y) → x
(eval '(QUOTE y)) → y
(eval y) →(list '+ 3 4)
(eval (eval y)) →(+ 3 4)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 14
![Page 102: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/102.jpg)
Examples of EVAL Ib
(eval (eval x)) →7
(eval '(eval x)) →(+ 3 4)
(setq y 'x) → x
(eval 'y) → x
(eval '(QUOTE y)) → y
(eval y) →(list '+ 3 4)
(eval (eval y))
→(+ 3 4)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 14
![Page 103: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/103.jpg)
Examples of EVAL Ib
(eval (eval x)) →7
(eval '(eval x)) →(+ 3 4)
(setq y 'x) → x
(eval 'y) → x
(eval '(QUOTE y)) → y
(eval y) →(list '+ 3 4)
(eval (eval y)) →(+ 3 4)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 14
![Page 104: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/104.jpg)
Examples of EVAL IIa
(EVAL 'x '( (y x) (z A) (x P)) )
→ P
(EVAL '(CONS (CAR x) y)'( (x (A B C)) (y (D E))) )
→ (A D E)
(EVAL '(QUOTE x) '( (y x) (z A) (x P)) )
→ x
( (LAMBDA (x c) (EVAL x c))'W'( (W A) (X B) ) )
→ A
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 15
![Page 105: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/105.jpg)
Examples of EVAL IIa
(EVAL 'x '( (y x) (z A) (x P)) ) → P
(EVAL '(CONS (CAR x) y)'( (x (A B C)) (y (D E))) )
→ (A D E)
(EVAL '(QUOTE x) '( (y x) (z A) (x P)) )
→ x
( (LAMBDA (x c) (EVAL x c))'W'( (W A) (X B) ) )
→ A
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 15
![Page 106: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/106.jpg)
Examples of EVAL IIa
(EVAL 'x '( (y x) (z A) (x P)) ) → P
(EVAL '(CONS (CAR x) y)'( (x (A B C)) (y (D E))) )
→ (A D E)
(EVAL '(QUOTE x) '( (y x) (z A) (x P)) )
→ x
( (LAMBDA (x c) (EVAL x c))'W'( (W A) (X B) ) )
→ A
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 15
![Page 107: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/107.jpg)
Examples of EVAL IIa
(EVAL 'x '( (y x) (z A) (x P)) ) → P
(EVAL '(CONS (CAR x) y)'( (x (A B C)) (y (D E))) )
→ (A D E)
(EVAL '(QUOTE x) '( (y x) (z A) (x P)) )
→ x
( (LAMBDA (x c) (EVAL x c))'W'( (W A) (X B) ) )
→ A
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 15
![Page 108: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/108.jpg)
Examples of EVAL IIa
(EVAL 'x '( (y x) (z A) (x P)) ) → P
(EVAL '(CONS (CAR x) y)'( (x (A B C)) (y (D E))) )
→ (A D E)
(EVAL '(QUOTE x) '( (y x) (z A) (x P)) )
→ x
( (LAMBDA (x c) (EVAL x c))'W'( (W A) (X B) ) )
→ A
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 15
![Page 109: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/109.jpg)
Examples of EVAL IIa
(EVAL 'x '( (y x) (z A) (x P)) ) → P
(EVAL '(CONS (CAR x) y)'( (x (A B C)) (y (D E))) )
→ (A D E)
(EVAL '(QUOTE x) '( (y x) (z A) (x P)) )
→ x
( (LAMBDA (x c) (EVAL x c))'W'( (W A) (X B) ) )
→ A
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 15
![Page 110: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/110.jpg)
Examples of EVAL IIa
(EVAL 'x '( (y x) (z A) (x P)) ) → P
(EVAL '(CONS (CAR x) y)'( (x (A B C)) (y (D E))) )
→ (A D E)
(EVAL '(QUOTE x) '( (y x) (z A) (x P)) )
→ x
( (LAMBDA (x c) (EVAL x c))'W'( (W A) (X B) ) )
→ A
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 15
![Page 111: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/111.jpg)
Examples of EVAL IIa
(EVAL 'x '( (y x) (z A) (x P)) ) → P
(EVAL '(CONS (CAR x) y)'( (x (A B C)) (y (D E))) )
→ (A D E)
(EVAL '(QUOTE x) '( (y x) (z A) (x P)) )
→ x
( (LAMBDA (x c) (EVAL x c))'W'( (W A) (X B) ) )
→ A
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 15
![Page 112: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/112.jpg)
Examples of EVAL IIb
( (LAMBDA (x c) (EVAL 'W c))'fred'( (W A) (X B) ) )
→ A
( (LAMBDA (x c) (EVAL x c))'(QUOTE W) '( (W A) (X B) ) )
→ W
( (LAMBDA (x c) (EVAL (EVAL x nil) c))'(QUOTE W) '( (W A) (X B) ) )
→ ATrick:> (eval '〈form〉)is the same as> 〈form〉That is: �eval� cancels �quote� ...
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 16
![Page 113: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/113.jpg)
Examples of EVAL IIb
( (LAMBDA (x c) (EVAL 'W c))'fred'( (W A) (X B) ) )
→ A
( (LAMBDA (x c) (EVAL x c))'(QUOTE W) '( (W A) (X B) ) )
→ W
( (LAMBDA (x c) (EVAL (EVAL x nil) c))'(QUOTE W) '( (W A) (X B) ) )
→ ATrick:> (eval '〈form〉)is the same as> 〈form〉That is: �eval� cancels �quote� ...
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 16
![Page 114: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/114.jpg)
Examples of EVAL IIb
( (LAMBDA (x c) (EVAL 'W c))'fred'( (W A) (X B) ) )
→ A
( (LAMBDA (x c) (EVAL x c))'(QUOTE W) '( (W A) (X B) ) )
→ W
( (LAMBDA (x c) (EVAL (EVAL x nil) c))'(QUOTE W) '( (W A) (X B) ) )
→ ATrick:> (eval '〈form〉)is the same as> 〈form〉That is: �eval� cancels �quote� ...
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 16
![Page 115: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/115.jpg)
Examples of EVAL IIb
( (LAMBDA (x c) (EVAL 'W c))'fred'( (W A) (X B) ) )
→ A
( (LAMBDA (x c) (EVAL x c))'(QUOTE W) '( (W A) (X B) ) )
→ W
( (LAMBDA (x c) (EVAL (EVAL x nil) c))'(QUOTE W) '( (W A) (X B) ) )
→ ATrick:> (eval '〈form〉)is the same as> 〈form〉That is: �eval� cancels �quote� ...
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 16
![Page 116: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/116.jpg)
Examples of EVAL IIb
( (LAMBDA (x c) (EVAL 'W c))'fred'( (W A) (X B) ) )
→ A
( (LAMBDA (x c) (EVAL x c))'(QUOTE W) '( (W A) (X B) ) )
→ W
( (LAMBDA (x c) (EVAL (EVAL x nil) c))'(QUOTE W) '( (W A) (X B) ) )
→ ATrick:> (eval '〈form〉)is the same as> 〈form〉That is: �eval� cancels �quote� ...
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 16
![Page 117: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/117.jpg)
Examples of EVAL IIb
( (LAMBDA (x c) (EVAL 'W c))'fred'( (W A) (X B) ) )
→ A
( (LAMBDA (x c) (EVAL x c))'(QUOTE W) '( (W A) (X B) ) )
→ W
( (LAMBDA (x c) (EVAL (EVAL x nil) c))'(QUOTE W) '( (W A) (X B) ) )
→ A
Trick:> (eval '〈form〉)is the same as> 〈form〉That is: �eval� cancels �quote� ...
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 16
![Page 118: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/118.jpg)
Examples of EVAL IIb
( (LAMBDA (x c) (EVAL 'W c))'fred'( (W A) (X B) ) )
→ A
( (LAMBDA (x c) (EVAL x c))'(QUOTE W) '( (W A) (X B) ) )
→ W
( (LAMBDA (x c) (EVAL (EVAL x nil) c))'(QUOTE W) '( (W A) (X B) ) )
→ ATrick:> (eval '〈form〉)is the same as> 〈form〉That is: �eval� cancels �quote� ...
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 16
![Page 119: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/119.jpg)
Extending the Language
I Common-Lisp de�nes(IF 〈test − form〉 〈true − form〉〈else − form〉)
I How could we de�ne this in terms of pure Lisp primitives?
I Our �rst try (DO NOT IMPLEMENT!):
(DEFUN my-if (testF trueF falseF)(COND (test trueF)
(t falseF)))
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 17
![Page 120: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/120.jpg)
Extending the Language
I Common-Lisp de�nes(IF 〈test − form〉 〈true − form〉〈else − form〉)
I How could we de�ne this in terms of pure Lisp primitives?
I Our �rst try (DO NOT IMPLEMENT!):
(DEFUN my-if (testF trueF falseF)(COND (test trueF)
(t falseF)))
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 17
![Page 121: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/121.jpg)
Extending the Language
I Common-Lisp de�nes(IF 〈test − form〉 〈true − form〉〈else − form〉)
I How could we de�ne this in terms of pure Lisp primitives?
I Our �rst try (DO NOT IMPLEMENT!):
(DEFUN my-if (testF trueF falseF)(COND (test trueF)
(t falseF)))
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 17
![Page 122: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/122.jpg)
Testing Naive IF
I Consider an application:
(setf x '(1 2))
(my-if (ATOM x) x (CAR x)) →
1
(setf x 'blah)
(my-if (ATOM x) x (CAR x))→ error 'blah is not a list
I Note (CAR x) is always evaluated!
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 18
![Page 123: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/123.jpg)
Testing Naive IF
I Consider an application:
(setf x '(1 2))
(my-if (ATOM x) x (CAR x)) → 1
(setf x 'blah)
(my-if (ATOM x) x (CAR x))→ error 'blah is not a list
I Note (CAR x) is always evaluated!
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 18
![Page 124: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/124.jpg)
Testing Naive IF
I Consider an application:
(setf x '(1 2))
(my-if (ATOM x) x (CAR x)) → 1
(setf x 'blah)
(my-if (ATOM x) x (CAR x))→
error 'blah is not a list
I Note (CAR x) is always evaluated!
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 18
![Page 125: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/125.jpg)
Testing Naive IF
I Consider an application:
(setf x '(1 2))
(my-if (ATOM x) x (CAR x)) → 1
(setf x 'blah)
(my-if (ATOM x) x (CAR x))→ error 'blah is not a list
I Note (CAR x) is always evaluated!
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 18
![Page 126: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/126.jpg)
Testing Naive IF
I Consider an application:
(setf x '(1 2))
(my-if (ATOM x) x (CAR x)) → 1
(setf x 'blah)
(my-if (ATOM x) x (CAR x))→ error 'blah is not a list
I Note (CAR x) is always evaluated!
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 18
![Page 127: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/127.jpg)
Custom Evaluation of Forms
I Solution: Custom control over evaluation of args
Eval 1st argif true: eval 2nd argif false: eval 3rd arg
I We seem to need a new special form
I But Lisp's set of special forms is closed
I Actually, there's another way:
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 19
![Page 128: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/128.jpg)
Custom Evaluation of Forms
I Solution: Custom control over evaluation of args
Eval 1st argif true: eval 2nd argif false: eval 3rd arg
I We seem to need a new special form
I But Lisp's set of special forms is closed
I Actually, there's another way:
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 19
![Page 129: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/129.jpg)
Custom Evaluation of Forms
I Solution: Custom control over evaluation of args
Eval 1st argif true: eval 2nd argif false: eval 3rd arg
I We seem to need a new special form
I But Lisp's set of special forms is closed
I Actually, there's another way:
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 19
![Page 130: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/130.jpg)
Custom Evaluation of Forms
I Solution: Custom control over evaluation of args
Eval 1st argif true: eval 2nd argif false: eval 3rd arg
I We seem to need a new special form
I But Lisp's set of special forms is closed
I Actually, there's another way:
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 19
![Page 131: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/131.jpg)
Macro-functions
I Macro-functions get the unevaluated form and context;and return a new form to be evaluated in its place
I Installing an ordinary function into the function symbol table(setf (symbol-function 'foo-fun)
(function (lambda ()(list '+ 1 2))))
(foo-fun) → (+ 1 2)I Installing a macro-function into the macro symbol table(setf (macro-function 'foo-mac)
(function (lambda (args ctx)(list '+ 1 2))))
(foo-mac) → 3
I Result of foo-mac is evaluated!
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 20
![Page 132: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/132.jpg)
Macro-functions
I Macro-functions get the unevaluated form and context;and return a new form to be evaluated in its place
I Installing an ordinary function into the function symbol table(setf (symbol-function 'foo-fun)
(function (lambda ()(list '+ 1 2))))
(foo-fun) → (+ 1 2)I Installing a macro-function into the macro symbol table(setf (macro-function 'foo-mac)
(function (lambda (args ctx)(list '+ 1 2))))
(foo-mac) → 3
I Result of foo-mac is evaluated!
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 20
![Page 133: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/133.jpg)
Macro-functions
I Macro-functions get the unevaluated form and context;and return a new form to be evaluated in its place
I Installing an ordinary function into the function symbol table(setf (symbol-function 'foo-fun)
(function (lambda ()(list '+ 1 2))))
(foo-fun) →
(+ 1 2)I Installing a macro-function into the macro symbol table(setf (macro-function 'foo-mac)
(function (lambda (args ctx)(list '+ 1 2))))
(foo-mac) → 3
I Result of foo-mac is evaluated!
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 20
![Page 134: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/134.jpg)
Macro-functions
I Macro-functions get the unevaluated form and context;and return a new form to be evaluated in its place
I Installing an ordinary function into the function symbol table(setf (symbol-function 'foo-fun)
(function (lambda ()(list '+ 1 2))))
(foo-fun) → (+ 1 2)
I Installing a macro-function into the macro symbol table(setf (macro-function 'foo-mac)
(function (lambda (args ctx)(list '+ 1 2))))
(foo-mac) → 3
I Result of foo-mac is evaluated!
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 20
![Page 135: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/135.jpg)
Macro-functions
I Macro-functions get the unevaluated form and context;and return a new form to be evaluated in its place
I Installing an ordinary function into the function symbol table(setf (symbol-function 'foo-fun)
(function (lambda ()(list '+ 1 2))))
(foo-fun) → (+ 1 2)I Installing a macro-function into the macro symbol table(setf (macro-function 'foo-mac)
(function (lambda (args ctx)(list '+ 1 2))))
(foo-mac) → 3
I Result of foo-mac is evaluated!
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 20
![Page 136: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/136.jpg)
Macro-functions
I Macro-functions get the unevaluated form and context;and return a new form to be evaluated in its place
I Installing an ordinary function into the function symbol table(setf (symbol-function 'foo-fun)
(function (lambda ()(list '+ 1 2))))
(foo-fun) → (+ 1 2)I Installing a macro-function into the macro symbol table(setf (macro-function 'foo-mac)
(function (lambda (args ctx)(list '+ 1 2))))
(foo-mac) →
3
I Result of foo-mac is evaluated!
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 20
![Page 137: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/137.jpg)
Macro-functions
I Macro-functions get the unevaluated form and context;and return a new form to be evaluated in its place
I Installing an ordinary function into the function symbol table(setf (symbol-function 'foo-fun)
(function (lambda ()(list '+ 1 2))))
(foo-fun) → (+ 1 2)I Installing a macro-function into the macro symbol table(setf (macro-function 'foo-mac)
(function (lambda (args ctx)(list '+ 1 2))))
(foo-mac) → 3
I Result of foo-mac is evaluated!
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 20
![Page 138: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/138.jpg)
Macro-functions
I Macro-functions get the unevaluated form and context;and return a new form to be evaluated in its place
I Installing an ordinary function into the function symbol table(setf (symbol-function 'foo-fun)
(function (lambda ()(list '+ 1 2))))
(foo-fun) → (+ 1 2)I Installing a macro-function into the macro symbol table(setf (macro-function 'foo-mac)
(function (lambda (args ctx)(list '+ 1 2))))
(foo-mac) → 3
I Result of foo-mac is evaluated!
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 20
![Page 139: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/139.jpg)
Macro-functions with Arguments
I Ordinary function installed into the function symbol table
(setf (symbol-function 'foo-fun)(function (lambda (a1 a2 a3)
(list a1 a2 a3)))))
(foo-fun 'cons 'a nil) →(cons a nil) ;; cons quoted!
I Macro-function installed in macro symbol table
(setf (macro-function 'foo-mac)(function (lambda (args ctx)
(let ((a1 (second args))(a2 (third args)) (a3 (fourth args)))
(list a1 a2 a3 ))))))(foo-mac cons 'a nil) → (a)
I Why skip (�rst args)? = macro name ⇒in�nite loop
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 21
![Page 140: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/140.jpg)
Macro-functions with Arguments
I Ordinary function installed into the function symbol table
(setf (symbol-function 'foo-fun)(function (lambda (a1 a2 a3)
(list a1 a2 a3)))))(foo-fun 'cons 'a nil) →
(cons a nil) ;; cons quoted!I Macro-function installed in macro symbol table
(setf (macro-function 'foo-mac)(function (lambda (args ctx)
(let ((a1 (second args))(a2 (third args)) (a3 (fourth args)))
(list a1 a2 a3 ))))))(foo-mac cons 'a nil) → (a)
I Why skip (�rst args)? = macro name ⇒in�nite loop
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 21
![Page 141: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/141.jpg)
Macro-functions with Arguments
I Ordinary function installed into the function symbol table
(setf (symbol-function 'foo-fun)(function (lambda (a1 a2 a3)
(list a1 a2 a3)))))(foo-fun 'cons 'a nil) →(cons a nil) ;; cons quoted!
I Macro-function installed in macro symbol table
(setf (macro-function 'foo-mac)(function (lambda (args ctx)
(let ((a1 (second args))(a2 (third args)) (a3 (fourth args)))
(list a1 a2 a3 ))))))(foo-mac cons 'a nil) → (a)
I Why skip (�rst args)? = macro name ⇒in�nite loop
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 21
![Page 142: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/142.jpg)
Macro-functions with Arguments
I Ordinary function installed into the function symbol table
(setf (symbol-function 'foo-fun)(function (lambda (a1 a2 a3)
(list a1 a2 a3)))))(foo-fun 'cons 'a nil) →(cons a nil) ;; cons quoted!
I Macro-function installed in macro symbol table
(setf (macro-function 'foo-mac)(function (lambda (args ctx)
(let ((a1 (second args))(a2 (third args)) (a3 (fourth args)))
(list a1 a2 a3 ))))))
(foo-mac cons 'a nil) → (a)I Why skip (�rst args)? = macro name ⇒in�nite loop
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 21
![Page 143: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/143.jpg)
Macro-functions with Arguments
I Ordinary function installed into the function symbol table
(setf (symbol-function 'foo-fun)(function (lambda (a1 a2 a3)
(list a1 a2 a3)))))(foo-fun 'cons 'a nil) →(cons a nil) ;; cons quoted!
I Macro-function installed in macro symbol table
(setf (macro-function 'foo-mac)(function (lambda (args ctx)
(let ((a1 (second args))(a2 (third args)) (a3 (fourth args)))
(list a1 a2 a3 ))))))(foo-mac cons 'a nil) →
(a)I Why skip (�rst args)? = macro name ⇒in�nite loop
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 21
![Page 144: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/144.jpg)
Macro-functions with Arguments
I Ordinary function installed into the function symbol table
(setf (symbol-function 'foo-fun)(function (lambda (a1 a2 a3)
(list a1 a2 a3)))))(foo-fun 'cons 'a nil) →(cons a nil) ;; cons quoted!
I Macro-function installed in macro symbol table
(setf (macro-function 'foo-mac)(function (lambda (args ctx)
(let ((a1 (second args))(a2 (third args)) (a3 (fourth args)))
(list a1 a2 a3 ))))))(foo-mac cons 'a nil) → (a)
I Why skip (�rst args)? = macro name ⇒in�nite loop
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 21
![Page 145: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/145.jpg)
Macro-functions with Arguments
I Ordinary function installed into the function symbol table
(setf (symbol-function 'foo-fun)(function (lambda (a1 a2 a3)
(list a1 a2 a3)))))(foo-fun 'cons 'a nil) →(cons a nil) ;; cons quoted!
I Macro-function installed in macro symbol table
(setf (macro-function 'foo-mac)(function (lambda (args ctx)
(let ((a1 (second args))(a2 (third args)) (a3 (fourth args)))
(list a1 a2 a3 ))))))(foo-mac cons 'a nil) → (a)
I Why skip (�rst args)? = macro name ⇒in�nite loopB. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 21
![Page 146: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/146.jpg)
The defmacro Special Form
I Powerful and convenient way to extend the language:
(defmacro name ( a1 . . . an ) 〈form〉)
I A macro has a list of formal arguments like LAMBDA orDEFUN
I Formal arguments are bound to unevaluated argumentssupplied
I The 〈form〉 is evaluated (〈form〉 may use arguments)
I The result of 〈form〉 is returned in place of the macro
I Lisp evaluates returned result
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 22
![Page 147: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/147.jpg)
The defmacro Special Form
I Powerful and convenient way to extend the language:
(defmacro name ( a1 . . . an ) 〈form〉)
I A macro has a list of formal arguments like LAMBDA orDEFUN
I Formal arguments are bound to unevaluated argumentssupplied
I The 〈form〉 is evaluated (〈form〉 may use arguments)
I The result of 〈form〉 is returned in place of the macro
I Lisp evaluates returned result
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 22
![Page 148: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/148.jpg)
The defmacro Special Form
I Powerful and convenient way to extend the language:
(defmacro name ( a1 . . . an ) 〈form〉)
I A macro has a list of formal arguments like LAMBDA orDEFUN
I Formal arguments are bound to unevaluated argumentssupplied
I The 〈form〉 is evaluated (〈form〉 may use arguments)
I The result of 〈form〉 is returned in place of the macro
I Lisp evaluates returned result
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 22
![Page 149: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/149.jpg)
The defmacro Special Form
I Powerful and convenient way to extend the language:
(defmacro name ( a1 . . . an ) 〈form〉)
I A macro has a list of formal arguments like LAMBDA orDEFUN
I Formal arguments are bound to unevaluated argumentssupplied
I The 〈form〉 is evaluated (〈form〉 may use arguments)
I The result of 〈form〉 is returned in place of the macro
I Lisp evaluates returned result
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 22
![Page 150: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/150.jpg)
The defmacro Special Form
I Powerful and convenient way to extend the language:
(defmacro name ( a1 . . . an ) 〈form〉)
I A macro has a list of formal arguments like LAMBDA orDEFUN
I Formal arguments are bound to unevaluated argumentssupplied
I The 〈form〉 is evaluated (〈form〉 may use arguments)
I The result of 〈form〉 is returned in place of the macro
I Lisp evaluates returned result
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 22
![Page 151: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/151.jpg)
The defmacro Special Form
I Powerful and convenient way to extend the language:
(defmacro name ( a1 . . . an ) 〈form〉)
I A macro has a list of formal arguments like LAMBDA orDEFUN
I Formal arguments are bound to unevaluated argumentssupplied
I The 〈form〉 is evaluated (〈form〉 may use arguments)
I The result of 〈form〉 is returned in place of the macro
I Lisp evaluates returned result
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 22
![Page 152: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/152.jpg)
The defmacro Special Form
I A function evaluates its body form and returns the result
(defun mystery-fun ()(list '+ 1 2 )))
(mystery-fun)→ (+ 1 2)
I A macro evaluates its body form and then evaluates the result
(defmacro mystery-mac ()(list '+ 1 2 ))
(mystery-mac)→ 3
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 23
![Page 153: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/153.jpg)
The defmacro Special Form
I A function evaluates its body form and returns the result
(defun mystery-fun ()(list '+ 1 2 )))
(mystery-fun)→
(+ 1 2)
I A macro evaluates its body form and then evaluates the result
(defmacro mystery-mac ()(list '+ 1 2 ))
(mystery-mac)→ 3
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 23
![Page 154: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/154.jpg)
The defmacro Special Form
I A function evaluates its body form and returns the result
(defun mystery-fun ()(list '+ 1 2 )))
(mystery-fun)→ (+ 1 2)
I A macro evaluates its body form and then evaluates the result
(defmacro mystery-mac ()(list '+ 1 2 ))
(mystery-mac)→ 3
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 23
![Page 155: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/155.jpg)
The defmacro Special Form
I A function evaluates its body form and returns the result
(defun mystery-fun ()(list '+ 1 2 )))
(mystery-fun)→ (+ 1 2)
I A macro evaluates its body form and then evaluates the result
(defmacro mystery-mac ()(list '+ 1 2 ))
(mystery-mac)→ 3
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 23
![Page 156: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/156.jpg)
The defmacro Special Form
I A function evaluates its body form and returns the result
(defun mystery-fun ()(list '+ 1 2 )))
(mystery-fun)→ (+ 1 2)
I A macro evaluates its body form and then evaluates the result
(defmacro mystery-mac ()(list '+ 1 2 ))
(mystery-mac)→
3
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 23
![Page 157: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/157.jpg)
The defmacro Special Form
I A function evaluates its body form and returns the result
(defun mystery-fun ()(list '+ 1 2 )))
(mystery-fun)→ (+ 1 2)
I A macro evaluates its body form and then evaluates the result
(defmacro mystery-mac ()(list '+ 1 2 ))
(mystery-mac)→ 3
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 23
![Page 158: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/158.jpg)
De�ning kwote
I De�ne your own quote function named 'kwote:
(defmacro kwote (s-expr) (list 'quote s-expr))
(kwote fred) → fred(list fred) → error: fred is unbound
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 24
![Page 159: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/159.jpg)
De�ning kwote
I De�ne your own quote function named 'kwote:
(defmacro kwote (s-expr) (list 'quote s-expr))(kwote fred) →
fred(list fred) → error: fred is unbound
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 24
![Page 160: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/160.jpg)
De�ning kwote
I De�ne your own quote function named 'kwote:
(defmacro kwote (s-expr) (list 'quote s-expr))(kwote fred) → fred
(list fred) → error: fred is unbound
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 24
![Page 161: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/161.jpg)
De�ning kwote
I De�ne your own quote function named 'kwote:
(defmacro kwote (s-expr) (list 'quote s-expr))(kwote fred) → fred(list fred) →
error: fred is unbound
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 24
![Page 162: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/162.jpg)
De�ning kwote
I De�ne your own quote function named 'kwote:
(defmacro kwote (s-expr) (list 'quote s-expr))(kwote fred) → fred(list fred) → error: fred is unbound
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 24
![Page 163: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/163.jpg)
Understanding kwote
(defmacro kwote (s-expr) (list 'quote s-expr))
ENTER EVAL (kwote fred)ENTER EVAL-MACRO kwoteBIND s-expr ←fredENTER EVAL (list 'quote s-expr)ENTER EVAL 'quoteEXIT → quoteENTER EVAL s-exprEXIT→ fred
EXIT EVAL list → (quote fred)EXIT EVAL-MACRO → (quote fred)ENTER EVAL (quote fred)EXIT EVAL → fred
EXIT EVAL →fred
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 25
![Page 164: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/164.jpg)
Understanding kwote
(defmacro kwote (s-expr) (list 'quote s-expr))ENTER EVAL (kwote fred)
ENTER EVAL-MACRO kwoteBIND s-expr ←fredENTER EVAL (list 'quote s-expr)ENTER EVAL 'quoteEXIT → quoteENTER EVAL s-exprEXIT→ fred
EXIT EVAL list → (quote fred)EXIT EVAL-MACRO → (quote fred)ENTER EVAL (quote fred)EXIT EVAL → fred
EXIT EVAL →fred
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 25
![Page 165: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/165.jpg)
Understanding kwote
(defmacro kwote (s-expr) (list 'quote s-expr))ENTER EVAL (kwote fred)ENTER EVAL-MACRO kwote
BIND s-expr ←fredENTER EVAL (list 'quote s-expr)ENTER EVAL 'quoteEXIT → quoteENTER EVAL s-exprEXIT→ fred
EXIT EVAL list → (quote fred)EXIT EVAL-MACRO → (quote fred)ENTER EVAL (quote fred)EXIT EVAL → fred
EXIT EVAL →fred
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 25
![Page 166: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/166.jpg)
Understanding kwote
(defmacro kwote (s-expr) (list 'quote s-expr))ENTER EVAL (kwote fred)ENTER EVAL-MACRO kwoteBIND s-expr ←fred
ENTER EVAL (list 'quote s-expr)ENTER EVAL 'quoteEXIT → quoteENTER EVAL s-exprEXIT→ fred
EXIT EVAL list → (quote fred)EXIT EVAL-MACRO → (quote fred)ENTER EVAL (quote fred)EXIT EVAL → fred
EXIT EVAL →fred
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 25
![Page 167: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/167.jpg)
Understanding kwote
(defmacro kwote (s-expr) (list 'quote s-expr))ENTER EVAL (kwote fred)ENTER EVAL-MACRO kwoteBIND s-expr ←fredENTER EVAL (list 'quote s-expr)
ENTER EVAL 'quoteEXIT → quoteENTER EVAL s-exprEXIT→ fred
EXIT EVAL list → (quote fred)EXIT EVAL-MACRO → (quote fred)ENTER EVAL (quote fred)EXIT EVAL → fred
EXIT EVAL →fred
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 25
![Page 168: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/168.jpg)
Understanding kwote
(defmacro kwote (s-expr) (list 'quote s-expr))ENTER EVAL (kwote fred)ENTER EVAL-MACRO kwoteBIND s-expr ←fredENTER EVAL (list 'quote s-expr)ENTER EVAL 'quote
EXIT → quoteENTER EVAL s-exprEXIT→ fred
EXIT EVAL list → (quote fred)EXIT EVAL-MACRO → (quote fred)ENTER EVAL (quote fred)EXIT EVAL → fred
EXIT EVAL →fred
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 25
![Page 169: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/169.jpg)
Understanding kwote
(defmacro kwote (s-expr) (list 'quote s-expr))ENTER EVAL (kwote fred)ENTER EVAL-MACRO kwoteBIND s-expr ←fredENTER EVAL (list 'quote s-expr)ENTER EVAL 'quoteEXIT → quote
ENTER EVAL s-exprEXIT→ fred
EXIT EVAL list → (quote fred)EXIT EVAL-MACRO → (quote fred)ENTER EVAL (quote fred)EXIT EVAL → fred
EXIT EVAL →fred
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 25
![Page 170: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/170.jpg)
Understanding kwote
(defmacro kwote (s-expr) (list 'quote s-expr))ENTER EVAL (kwote fred)ENTER EVAL-MACRO kwoteBIND s-expr ←fredENTER EVAL (list 'quote s-expr)ENTER EVAL 'quoteEXIT → quoteENTER EVAL s-expr
EXIT→ fredEXIT EVAL list → (quote fred)
EXIT EVAL-MACRO → (quote fred)ENTER EVAL (quote fred)EXIT EVAL → fred
EXIT EVAL →fred
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 25
![Page 171: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/171.jpg)
Understanding kwote
(defmacro kwote (s-expr) (list 'quote s-expr))ENTER EVAL (kwote fred)ENTER EVAL-MACRO kwoteBIND s-expr ←fredENTER EVAL (list 'quote s-expr)ENTER EVAL 'quoteEXIT → quoteENTER EVAL s-exprEXIT→ fred
EXIT EVAL list → (quote fred)EXIT EVAL-MACRO → (quote fred)ENTER EVAL (quote fred)EXIT EVAL → fred
EXIT EVAL →fred
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 25
![Page 172: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/172.jpg)
Understanding kwote
(defmacro kwote (s-expr) (list 'quote s-expr))ENTER EVAL (kwote fred)ENTER EVAL-MACRO kwoteBIND s-expr ←fredENTER EVAL (list 'quote s-expr)ENTER EVAL 'quoteEXIT → quoteENTER EVAL s-exprEXIT→ fred
EXIT EVAL list → (quote fred)
EXIT EVAL-MACRO → (quote fred)ENTER EVAL (quote fred)EXIT EVAL → fred
EXIT EVAL →fred
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 25
![Page 173: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/173.jpg)
Understanding kwote
(defmacro kwote (s-expr) (list 'quote s-expr))ENTER EVAL (kwote fred)ENTER EVAL-MACRO kwoteBIND s-expr ←fredENTER EVAL (list 'quote s-expr)ENTER EVAL 'quoteEXIT → quoteENTER EVAL s-exprEXIT→ fred
EXIT EVAL list → (quote fred)EXIT EVAL-MACRO → (quote fred)
ENTER EVAL (quote fred)EXIT EVAL → fred
EXIT EVAL →fred
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 25
![Page 174: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/174.jpg)
Understanding kwote
(defmacro kwote (s-expr) (list 'quote s-expr))ENTER EVAL (kwote fred)ENTER EVAL-MACRO kwoteBIND s-expr ←fredENTER EVAL (list 'quote s-expr)ENTER EVAL 'quoteEXIT → quoteENTER EVAL s-exprEXIT→ fred
EXIT EVAL list → (quote fred)EXIT EVAL-MACRO → (quote fred)ENTER EVAL (quote fred)
EXIT EVAL → fredEXIT EVAL →fred
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 25
![Page 175: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/175.jpg)
Understanding kwote
(defmacro kwote (s-expr) (list 'quote s-expr))ENTER EVAL (kwote fred)ENTER EVAL-MACRO kwoteBIND s-expr ←fredENTER EVAL (list 'quote s-expr)ENTER EVAL 'quoteEXIT → quoteENTER EVAL s-exprEXIT→ fred
EXIT EVAL list → (quote fred)EXIT EVAL-MACRO → (quote fred)ENTER EVAL (quote fred)EXIT EVAL → fred
EXIT EVAL →fred
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 25
![Page 176: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/176.jpg)
Understanding kwote
(defmacro kwote (s-expr) (list 'quote s-expr))ENTER EVAL (kwote fred)ENTER EVAL-MACRO kwoteBIND s-expr ←fredENTER EVAL (list 'quote s-expr)ENTER EVAL 'quoteEXIT → quoteENTER EVAL s-exprEXIT→ fred
EXIT EVAL list → (quote fred)EXIT EVAL-MACRO → (quote fred)ENTER EVAL (quote fred)EXIT EVAL → fred
EXIT EVAL →fred
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 25
![Page 177: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/177.jpg)
�backquote� facility
I Concise clean way to handle code generation with arguments
I The backquote ` introduces a �template�
I The comma , introduces substituable parameters
I The substitutions are evaluated once
I Compare versions
(defmacro kwote (s-expr) (list 'quote s-expr))(defmacro kwote (s-expr) `(quote ,s-expr))(defmacro kwote (s-expr) `',s-expr)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 26
![Page 178: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/178.jpg)
�backquote� facility
I Concise clean way to handle code generation with arguments
I The backquote ` introduces a �template�
I The comma , introduces substituable parameters
I The substitutions are evaluated once
I Compare versions
(defmacro kwote (s-expr) (list 'quote s-expr))(defmacro kwote (s-expr) `(quote ,s-expr))(defmacro kwote (s-expr) `',s-expr)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 26
![Page 179: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/179.jpg)
�backquote� facility
I Concise clean way to handle code generation with arguments
I The backquote ` introduces a �template�
I The comma , introduces substituable parameters
I The substitutions are evaluated once
I Compare versions
(defmacro kwote (s-expr) (list 'quote s-expr))(defmacro kwote (s-expr) `(quote ,s-expr))(defmacro kwote (s-expr) `',s-expr)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 26
![Page 180: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/180.jpg)
�backquote� facility
I Concise clean way to handle code generation with arguments
I The backquote ` introduces a �template�
I The comma , introduces substituable parameters
I The substitutions are evaluated once
I Compare versions
(defmacro kwote (s-expr) (list 'quote s-expr))(defmacro kwote (s-expr) `(quote ,s-expr))(defmacro kwote (s-expr) `',s-expr)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 26
![Page 181: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/181.jpg)
�backquote� facility
I Concise clean way to handle code generation with arguments
I The backquote ` introduces a �template�
I The comma , introduces substituable parameters
I The substitutions are evaluated once
I Compare versions
(defmacro kwote (s-expr) (list 'quote s-expr))(defmacro kwote (s-expr) `(quote ,s-expr))(defmacro kwote (s-expr) `',s-expr)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 26
![Page 182: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/182.jpg)
defmacro using backquote for arguments
(defmacro greet (name) `'( hello ,name ! ))
(greet richard) →( hello richard ! ) ;; note: richard unquoted
(greet (/ 0 0) ) → ( hello (/ 0 0) ! )
(defmacro my-if (testF trueF falseF)`(cond (,testF ,trueF)
( t ,falseF)))
(my-if t 'ok (/ 0 0)) → ok(my-if nil 'ok (/ 0 0)) →error: zero divisor
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 27
![Page 183: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/183.jpg)
defmacro using backquote for arguments
(defmacro greet (name) `'( hello ,name ! ))
(greet richard) →
( hello richard ! ) ;; note: richard unquoted
(greet (/ 0 0) ) → ( hello (/ 0 0) ! )
(defmacro my-if (testF trueF falseF)`(cond (,testF ,trueF)
( t ,falseF)))
(my-if t 'ok (/ 0 0)) → ok(my-if nil 'ok (/ 0 0)) →error: zero divisor
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 27
![Page 184: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/184.jpg)
defmacro using backquote for arguments
(defmacro greet (name) `'( hello ,name ! ))
(greet richard) →( hello richard ! ) ;; note: richard unquoted
(greet (/ 0 0) ) → ( hello (/ 0 0) ! )
(defmacro my-if (testF trueF falseF)`(cond (,testF ,trueF)
( t ,falseF)))
(my-if t 'ok (/ 0 0)) → ok(my-if nil 'ok (/ 0 0)) →error: zero divisor
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 27
![Page 185: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/185.jpg)
defmacro using backquote for arguments
(defmacro greet (name) `'( hello ,name ! ))
(greet richard) →( hello richard ! ) ;; note: richard unquoted
(greet (/ 0 0) ) →
( hello (/ 0 0) ! )
(defmacro my-if (testF trueF falseF)`(cond (,testF ,trueF)
( t ,falseF)))
(my-if t 'ok (/ 0 0)) → ok(my-if nil 'ok (/ 0 0)) →error: zero divisor
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 27
![Page 186: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/186.jpg)
defmacro using backquote for arguments
(defmacro greet (name) `'( hello ,name ! ))
(greet richard) →( hello richard ! ) ;; note: richard unquoted
(greet (/ 0 0) ) → ( hello (/ 0 0) ! )
(defmacro my-if (testF trueF falseF)`(cond (,testF ,trueF)
( t ,falseF)))
(my-if t 'ok (/ 0 0)) → ok(my-if nil 'ok (/ 0 0)) →error: zero divisor
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 27
![Page 187: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/187.jpg)
defmacro using backquote for arguments
(defmacro greet (name) `'( hello ,name ! ))
(greet richard) →( hello richard ! ) ;; note: richard unquoted
(greet (/ 0 0) ) → ( hello (/ 0 0) ! )
(defmacro my-if (testF trueF falseF)`(cond (,testF ,trueF)
( t ,falseF)))
(my-if t 'ok (/ 0 0)) → ok(my-if nil 'ok (/ 0 0)) →error: zero divisor
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 27
![Page 188: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/188.jpg)
defmacro using backquote for arguments
(defmacro greet (name) `'( hello ,name ! ))
(greet richard) →( hello richard ! ) ;; note: richard unquoted
(greet (/ 0 0) ) → ( hello (/ 0 0) ! )
(defmacro my-if (testF trueF falseF)`(cond (,testF ,trueF)
( t ,falseF)))
(my-if t 'ok (/ 0 0)) →
ok(my-if nil 'ok (/ 0 0)) →error: zero divisor
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 27
![Page 189: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/189.jpg)
defmacro using backquote for arguments
(defmacro greet (name) `'( hello ,name ! ))
(greet richard) →( hello richard ! ) ;; note: richard unquoted
(greet (/ 0 0) ) → ( hello (/ 0 0) ! )
(defmacro my-if (testF trueF falseF)`(cond (,testF ,trueF)
( t ,falseF)))
(my-if t 'ok (/ 0 0)) → ok
(my-if nil 'ok (/ 0 0)) →error: zero divisor
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 27
![Page 190: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/190.jpg)
defmacro using backquote for arguments
(defmacro greet (name) `'( hello ,name ! ))
(greet richard) →( hello richard ! ) ;; note: richard unquoted
(greet (/ 0 0) ) → ( hello (/ 0 0) ! )
(defmacro my-if (testF trueF falseF)`(cond (,testF ,trueF)
( t ,falseF)))
(my-if t 'ok (/ 0 0)) → ok(my-if nil 'ok (/ 0 0)) →
error: zero divisor
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 27
![Page 191: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/191.jpg)
defmacro using backquote for arguments
(defmacro greet (name) `'( hello ,name ! ))
(greet richard) →( hello richard ! ) ;; note: richard unquoted
(greet (/ 0 0) ) → ( hello (/ 0 0) ! )
(defmacro my-if (testF trueF falseF)`(cond (,testF ,trueF)
( t ,falseF)))
(my-if t 'ok (/ 0 0)) → ok(my-if nil 'ok (/ 0 0)) →error: zero divisor
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 27
![Page 192: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/192.jpg)
Introducing local variables in macros
(defmacroarithmetic-if (test neg-form zero-form pos-form)
(let ((var (gensym)))`(let ((,var ,test))
(cond ((< ,var 0) ,neg-form)((= ,var 0) ,zero-form)( t ,pos-form)))))
I gensym creates a new variable name
I This name is guaranteed not to be used already
I It cannot shadow variables in the neg, zero and pos forms
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 28
![Page 193: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/193.jpg)
Introducing local variables in macros
(defmacroarithmetic-if (test neg-form zero-form pos-form)
(let ((var (gensym)))`(let ((,var ,test))
(cond ((< ,var 0) ,neg-form)((= ,var 0) ,zero-form)( t ,pos-form)))))
I gensym creates a new variable name
I This name is guaranteed not to be used already
I It cannot shadow variables in the neg, zero and pos forms
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 28
![Page 194: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/194.jpg)
Introducing local variables in macros
(defmacroarithmetic-if (test neg-form zero-form pos-form)
(let ((var (gensym)))`(let ((,var ,test))
(cond ((< ,var 0) ,neg-form)((= ,var 0) ,zero-form)( t ,pos-form)))))
I gensym creates a new variable name
I This name is guaranteed not to be used already
I It cannot shadow variables in the neg, zero and pos forms
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 28
![Page 195: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/195.jpg)
Introducing local variables in macros
(defmacroarithmetic-if (test neg-form zero-form pos-form)
(let ((var (gensym)))`(let ((,var ,test))
(cond ((< ,var 0) ,neg-form)((= ,var 0) ,zero-form)( t ,pos-form)))))
I gensym creates a new variable name
I This name is guaranteed not to be used already
I It cannot shadow variables in the neg, zero and pos forms
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 28
![Page 196: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/196.jpg)
Variable length argument lists
I Like defun, defmacro accepts the &rest keyword
(defmacro random-form (&rest args)(nth (random (length args)) args) )
(random-form (car '(a b)) (+ 1 2) ) → A(random-form (car '(a b)) (+ 1 2) ) → A(random-form (car '(a b)) (+ 1 2) ) → 3(random-form (car '(a b)) (+ 1 2) ) → A
I Also works on this list
(random-form 'a x (- 27) (length '(t u x)) ) →-27(random-form 'a x (- 27) (length '(t u x)) )→ error: x undefined
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 29
![Page 197: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/197.jpg)
Variable length argument lists
I Like defun, defmacro accepts the &rest keyword
(defmacro random-form (&rest args)(nth (random (length args)) args) )
(random-form (car '(a b)) (+ 1 2) ) →
A(random-form (car '(a b)) (+ 1 2) ) → A(random-form (car '(a b)) (+ 1 2) ) → 3(random-form (car '(a b)) (+ 1 2) ) → A
I Also works on this list
(random-form 'a x (- 27) (length '(t u x)) ) →-27(random-form 'a x (- 27) (length '(t u x)) )→ error: x undefined
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 29
![Page 198: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/198.jpg)
Variable length argument lists
I Like defun, defmacro accepts the &rest keyword
(defmacro random-form (&rest args)(nth (random (length args)) args) )
(random-form (car '(a b)) (+ 1 2) ) → A
(random-form (car '(a b)) (+ 1 2) ) → A(random-form (car '(a b)) (+ 1 2) ) → 3(random-form (car '(a b)) (+ 1 2) ) → A
I Also works on this list
(random-form 'a x (- 27) (length '(t u x)) ) →-27(random-form 'a x (- 27) (length '(t u x)) )→ error: x undefined
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 29
![Page 199: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/199.jpg)
Variable length argument lists
I Like defun, defmacro accepts the &rest keyword
(defmacro random-form (&rest args)(nth (random (length args)) args) )
(random-form (car '(a b)) (+ 1 2) ) → A(random-form (car '(a b)) (+ 1 2) ) →
A(random-form (car '(a b)) (+ 1 2) ) → 3(random-form (car '(a b)) (+ 1 2) ) → A
I Also works on this list
(random-form 'a x (- 27) (length '(t u x)) ) →-27(random-form 'a x (- 27) (length '(t u x)) )→ error: x undefined
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 29
![Page 200: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/200.jpg)
Variable length argument lists
I Like defun, defmacro accepts the &rest keyword
(defmacro random-form (&rest args)(nth (random (length args)) args) )
(random-form (car '(a b)) (+ 1 2) ) → A(random-form (car '(a b)) (+ 1 2) ) → A
(random-form (car '(a b)) (+ 1 2) ) → 3(random-form (car '(a b)) (+ 1 2) ) → A
I Also works on this list
(random-form 'a x (- 27) (length '(t u x)) ) →-27(random-form 'a x (- 27) (length '(t u x)) )→ error: x undefined
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 29
![Page 201: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/201.jpg)
Variable length argument lists
I Like defun, defmacro accepts the &rest keyword
(defmacro random-form (&rest args)(nth (random (length args)) args) )
(random-form (car '(a b)) (+ 1 2) ) → A(random-form (car '(a b)) (+ 1 2) ) → A(random-form (car '(a b)) (+ 1 2) ) →
3(random-form (car '(a b)) (+ 1 2) ) → A
I Also works on this list
(random-form 'a x (- 27) (length '(t u x)) ) →-27(random-form 'a x (- 27) (length '(t u x)) )→ error: x undefined
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 29
![Page 202: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/202.jpg)
Variable length argument lists
I Like defun, defmacro accepts the &rest keyword
(defmacro random-form (&rest args)(nth (random (length args)) args) )
(random-form (car '(a b)) (+ 1 2) ) → A(random-form (car '(a b)) (+ 1 2) ) → A(random-form (car '(a b)) (+ 1 2) ) → 3
(random-form (car '(a b)) (+ 1 2) ) → A
I Also works on this list
(random-form 'a x (- 27) (length '(t u x)) ) →-27(random-form 'a x (- 27) (length '(t u x)) )→ error: x undefined
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 29
![Page 203: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/203.jpg)
Variable length argument lists
I Like defun, defmacro accepts the &rest keyword
(defmacro random-form (&rest args)(nth (random (length args)) args) )
(random-form (car '(a b)) (+ 1 2) ) → A(random-form (car '(a b)) (+ 1 2) ) → A(random-form (car '(a b)) (+ 1 2) ) → 3(random-form (car '(a b)) (+ 1 2) ) →
A
I Also works on this list
(random-form 'a x (- 27) (length '(t u x)) ) →-27(random-form 'a x (- 27) (length '(t u x)) )→ error: x undefined
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 29
![Page 204: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/204.jpg)
Variable length argument lists
I Like defun, defmacro accepts the &rest keyword
(defmacro random-form (&rest args)(nth (random (length args)) args) )
(random-form (car '(a b)) (+ 1 2) ) → A(random-form (car '(a b)) (+ 1 2) ) → A(random-form (car '(a b)) (+ 1 2) ) → 3(random-form (car '(a b)) (+ 1 2) ) → A
I Also works on this list
(random-form 'a x (- 27) (length '(t u x)) ) →-27(random-form 'a x (- 27) (length '(t u x)) )→ error: x undefined
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 29
![Page 205: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/205.jpg)
Variable length argument lists
I Like defun, defmacro accepts the &rest keyword
(defmacro random-form (&rest args)(nth (random (length args)) args) )
(random-form (car '(a b)) (+ 1 2) ) → A(random-form (car '(a b)) (+ 1 2) ) → A(random-form (car '(a b)) (+ 1 2) ) → 3(random-form (car '(a b)) (+ 1 2) ) → A
I Also works on this list
(random-form 'a x (- 27) (length '(t u x)) ) →
-27(random-form 'a x (- 27) (length '(t u x)) )→ error: x undefined
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 29
![Page 206: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/206.jpg)
Variable length argument lists
I Like defun, defmacro accepts the &rest keyword
(defmacro random-form (&rest args)(nth (random (length args)) args) )
(random-form (car '(a b)) (+ 1 2) ) → A(random-form (car '(a b)) (+ 1 2) ) → A(random-form (car '(a b)) (+ 1 2) ) → 3(random-form (car '(a b)) (+ 1 2) ) → A
I Also works on this list
(random-form 'a x (- 27) (length '(t u x)) ) →-27
(random-form 'a x (- 27) (length '(t u x)) )→ error: x undefined
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 29
![Page 207: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/207.jpg)
Variable length argument lists
I Like defun, defmacro accepts the &rest keyword
(defmacro random-form (&rest args)(nth (random (length args)) args) )
(random-form (car '(a b)) (+ 1 2) ) → A(random-form (car '(a b)) (+ 1 2) ) → A(random-form (car '(a b)) (+ 1 2) ) → 3(random-form (car '(a b)) (+ 1 2) ) → A
I Also works on this list
(random-form 'a x (- 27) (length '(t u x)) ) →-27(random-form 'a x (- 27) (length '(t u x)) )→
error: x undefined
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 29
![Page 208: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/208.jpg)
Variable length argument lists
I Like defun, defmacro accepts the &rest keyword
(defmacro random-form (&rest args)(nth (random (length args)) args) )
(random-form (car '(a b)) (+ 1 2) ) → A(random-form (car '(a b)) (+ 1 2) ) → A(random-form (car '(a b)) (+ 1 2) ) → 3(random-form (car '(a b)) (+ 1 2) ) → A
I Also works on this list
(random-form 'a x (- 27) (length '(t u x)) ) →-27(random-form 'a x (- 27) (length '(t u x)) )→ error: x undefined
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 29
![Page 209: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/209.jpg)
Comments on Macros
I Macros are not functions: they do not evaluate their arguments
I Macros are not special forms
I Lisp de�nes a �xed set of special forms that evaluatearguments in special ways
I Macros can alter its arguments, but must eventually express itscomputation in terms of special forms
I Macros may call other macros
I SETF is a macro
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 30
![Page 210: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/210.jpg)
Comments on Macros
I Macros are not functions: they do not evaluate their arguments
I Macros are not special forms
I Lisp de�nes a �xed set of special forms that evaluatearguments in special ways
I Macros can alter its arguments, but must eventually express itscomputation in terms of special forms
I Macros may call other macros
I SETF is a macro
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 30
![Page 211: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/211.jpg)
Comments on Macros
I Macros are not functions: they do not evaluate their arguments
I Macros are not special formsI Lisp de�nes a �xed set of special forms that evaluate
arguments in special ways
I Macros can alter its arguments, but must eventually express itscomputation in terms of special forms
I Macros may call other macros
I SETF is a macro
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 30
![Page 212: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/212.jpg)
Comments on Macros
I Macros are not functions: they do not evaluate their arguments
I Macros are not special formsI Lisp de�nes a �xed set of special forms that evaluate
arguments in special waysI Macros can alter its arguments, but must eventually express its
computation in terms of special forms
I Macros may call other macros
I SETF is a macro
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 30
![Page 213: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/213.jpg)
Comments on Macros
I Macros are not functions: they do not evaluate their arguments
I Macros are not special formsI Lisp de�nes a �xed set of special forms that evaluate
arguments in special waysI Macros can alter its arguments, but must eventually express its
computation in terms of special forms
I Macros may call other macros
I SETF is a macro
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 30
![Page 214: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/214.jpg)
Comments on Macros
I Macros are not functions: they do not evaluate their arguments
I Macros are not special formsI Lisp de�nes a �xed set of special forms that evaluate
arguments in special waysI Macros can alter its arguments, but must eventually express its
computation in terms of special forms
I Macros may call other macros
I SETF is a macro
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 30
![Page 215: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/215.jpg)
NLAMBDA and FEXPR
I Found in �classic� lisps
I No longer supported in common lisp and use is discouraged
I Interfere with compiler optimizationI Can interact strangely with dynamic scoping
I NLAMBDA is identical to LAMBDA but does not evaluatearguments before passing them to the body( (LAMBDA (x) 'ok ) (/ 0 0) ) →error: divide by zero( (NLAMBDA (x) 'ok ) (/ 0 0) ) → 'ok( (NLAMBDA (x) x ) (/ 0 0) ) → (/ 0 0)
I To evaluate arguments, you must explicitly call eval
( (NLAMBDA (x) (eval x)) (/ 0 0) ) →error: divide by zeroI In contrast, macros always pass result to evaluator
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 31
![Page 216: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/216.jpg)
NLAMBDA and FEXPR
I Found in �classic� lisps
I No longer supported in common lisp and use is discouraged
I Interfere with compiler optimizationI Can interact strangely with dynamic scoping
I NLAMBDA is identical to LAMBDA but does not evaluatearguments before passing them to the body( (LAMBDA (x) 'ok ) (/ 0 0) ) →error: divide by zero( (NLAMBDA (x) 'ok ) (/ 0 0) ) → 'ok( (NLAMBDA (x) x ) (/ 0 0) ) → (/ 0 0)
I To evaluate arguments, you must explicitly call eval
( (NLAMBDA (x) (eval x)) (/ 0 0) ) →error: divide by zeroI In contrast, macros always pass result to evaluator
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 31
![Page 217: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/217.jpg)
NLAMBDA and FEXPR
I Found in �classic� lisps
I No longer supported in common lisp and use is discouragedI Interfere with compiler optimization
I Can interact strangely with dynamic scoping
I NLAMBDA is identical to LAMBDA but does not evaluatearguments before passing them to the body( (LAMBDA (x) 'ok ) (/ 0 0) ) →error: divide by zero( (NLAMBDA (x) 'ok ) (/ 0 0) ) → 'ok( (NLAMBDA (x) x ) (/ 0 0) ) → (/ 0 0)
I To evaluate arguments, you must explicitly call eval
( (NLAMBDA (x) (eval x)) (/ 0 0) ) →error: divide by zeroI In contrast, macros always pass result to evaluator
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 31
![Page 218: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/218.jpg)
NLAMBDA and FEXPR
I Found in �classic� lisps
I No longer supported in common lisp and use is discouragedI Interfere with compiler optimizationI Can interact strangely with dynamic scoping
I NLAMBDA is identical to LAMBDA but does not evaluatearguments before passing them to the body( (LAMBDA (x) 'ok ) (/ 0 0) ) →error: divide by zero( (NLAMBDA (x) 'ok ) (/ 0 0) ) → 'ok( (NLAMBDA (x) x ) (/ 0 0) ) → (/ 0 0)
I To evaluate arguments, you must explicitly call eval
( (NLAMBDA (x) (eval x)) (/ 0 0) ) →error: divide by zeroI In contrast, macros always pass result to evaluator
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 31
![Page 219: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/219.jpg)
NLAMBDA and FEXPR
I Found in �classic� lisps
I No longer supported in common lisp and use is discouragedI Interfere with compiler optimizationI Can interact strangely with dynamic scoping
I NLAMBDA is identical to LAMBDA but does not evaluatearguments before passing them to the body
( (LAMBDA (x) 'ok ) (/ 0 0) ) →error: divide by zero( (NLAMBDA (x) 'ok ) (/ 0 0) ) → 'ok( (NLAMBDA (x) x ) (/ 0 0) ) → (/ 0 0)
I To evaluate arguments, you must explicitly call eval
( (NLAMBDA (x) (eval x)) (/ 0 0) ) →error: divide by zeroI In contrast, macros always pass result to evaluator
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 31
![Page 220: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/220.jpg)
NLAMBDA and FEXPR
I Found in �classic� lisps
I No longer supported in common lisp and use is discouragedI Interfere with compiler optimizationI Can interact strangely with dynamic scoping
I NLAMBDA is identical to LAMBDA but does not evaluatearguments before passing them to the body( (LAMBDA (x) 'ok ) (/ 0 0) ) →
error: divide by zero( (NLAMBDA (x) 'ok ) (/ 0 0) ) → 'ok( (NLAMBDA (x) x ) (/ 0 0) ) → (/ 0 0)
I To evaluate arguments, you must explicitly call eval
( (NLAMBDA (x) (eval x)) (/ 0 0) ) →error: divide by zeroI In contrast, macros always pass result to evaluator
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 31
![Page 221: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/221.jpg)
NLAMBDA and FEXPR
I Found in �classic� lisps
I No longer supported in common lisp and use is discouragedI Interfere with compiler optimizationI Can interact strangely with dynamic scoping
I NLAMBDA is identical to LAMBDA but does not evaluatearguments before passing them to the body( (LAMBDA (x) 'ok ) (/ 0 0) ) →error: divide by zero
( (NLAMBDA (x) 'ok ) (/ 0 0) ) → 'ok( (NLAMBDA (x) x ) (/ 0 0) ) → (/ 0 0)
I To evaluate arguments, you must explicitly call eval
( (NLAMBDA (x) (eval x)) (/ 0 0) ) →error: divide by zeroI In contrast, macros always pass result to evaluator
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 31
![Page 222: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/222.jpg)
NLAMBDA and FEXPR
I Found in �classic� lisps
I No longer supported in common lisp and use is discouragedI Interfere with compiler optimizationI Can interact strangely with dynamic scoping
I NLAMBDA is identical to LAMBDA but does not evaluatearguments before passing them to the body( (LAMBDA (x) 'ok ) (/ 0 0) ) →error: divide by zero( (NLAMBDA (x) 'ok ) (/ 0 0) ) →
'ok( (NLAMBDA (x) x ) (/ 0 0) ) → (/ 0 0)
I To evaluate arguments, you must explicitly call eval
( (NLAMBDA (x) (eval x)) (/ 0 0) ) →error: divide by zeroI In contrast, macros always pass result to evaluator
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 31
![Page 223: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/223.jpg)
NLAMBDA and FEXPR
I Found in �classic� lisps
I No longer supported in common lisp and use is discouragedI Interfere with compiler optimizationI Can interact strangely with dynamic scoping
I NLAMBDA is identical to LAMBDA but does not evaluatearguments before passing them to the body( (LAMBDA (x) 'ok ) (/ 0 0) ) →error: divide by zero( (NLAMBDA (x) 'ok ) (/ 0 0) ) → 'ok
( (NLAMBDA (x) x ) (/ 0 0) ) → (/ 0 0)I To evaluate arguments, you must explicitly call eval
( (NLAMBDA (x) (eval x)) (/ 0 0) ) →error: divide by zeroI In contrast, macros always pass result to evaluator
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 31
![Page 224: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/224.jpg)
NLAMBDA and FEXPR
I Found in �classic� lisps
I No longer supported in common lisp and use is discouragedI Interfere with compiler optimizationI Can interact strangely with dynamic scoping
I NLAMBDA is identical to LAMBDA but does not evaluatearguments before passing them to the body( (LAMBDA (x) 'ok ) (/ 0 0) ) →error: divide by zero( (NLAMBDA (x) 'ok ) (/ 0 0) ) → 'ok( (NLAMBDA (x) x ) (/ 0 0) ) →
(/ 0 0)I To evaluate arguments, you must explicitly call eval
( (NLAMBDA (x) (eval x)) (/ 0 0) ) →error: divide by zeroI In contrast, macros always pass result to evaluator
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 31
![Page 225: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/225.jpg)
NLAMBDA and FEXPR
I Found in �classic� lisps
I No longer supported in common lisp and use is discouragedI Interfere with compiler optimizationI Can interact strangely with dynamic scoping
I NLAMBDA is identical to LAMBDA but does not evaluatearguments before passing them to the body( (LAMBDA (x) 'ok ) (/ 0 0) ) →error: divide by zero( (NLAMBDA (x) 'ok ) (/ 0 0) ) → 'ok( (NLAMBDA (x) x ) (/ 0 0) ) → (/ 0 0)
I To evaluate arguments, you must explicitly call eval
( (NLAMBDA (x) (eval x)) (/ 0 0) ) →error: divide by zeroI In contrast, macros always pass result to evaluator
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 31
![Page 226: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/226.jpg)
NLAMBDA and FEXPR
I Found in �classic� lisps
I No longer supported in common lisp and use is discouragedI Interfere with compiler optimizationI Can interact strangely with dynamic scoping
I NLAMBDA is identical to LAMBDA but does not evaluatearguments before passing them to the body( (LAMBDA (x) 'ok ) (/ 0 0) ) →error: divide by zero( (NLAMBDA (x) 'ok ) (/ 0 0) ) → 'ok( (NLAMBDA (x) x ) (/ 0 0) ) → (/ 0 0)
I To evaluate arguments, you must explicitly call eval
( (NLAMBDA (x) (eval x)) (/ 0 0) ) →error: divide by zero
I In contrast, macros always pass result to evaluator
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 31
![Page 227: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/227.jpg)
NLAMBDA and FEXPR
I Found in �classic� lisps
I No longer supported in common lisp and use is discouragedI Interfere with compiler optimizationI Can interact strangely with dynamic scoping
I NLAMBDA is identical to LAMBDA but does not evaluatearguments before passing them to the body( (LAMBDA (x) 'ok ) (/ 0 0) ) →error: divide by zero( (NLAMBDA (x) 'ok ) (/ 0 0) ) → 'ok( (NLAMBDA (x) x ) (/ 0 0) ) → (/ 0 0)
I To evaluate arguments, you must explicitly call eval
( (NLAMBDA (x) (eval x)) (/ 0 0) ) →error: divide by zeroI In contrast, macros always pass result to evaluator
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 31
![Page 228: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/228.jpg)
EVAL vs. APPLY
I APPLY does for functions what EVAL does for forms.
I APPLY takes a function name, a list of arguments, and acontext; andapplies the function to the arguments (using context asneeded).
I EVAL: form + context ; s-exprAPPLY: function + args + context ; s-expr
(f s1 ... sn) ⇔ (APPLY 'f '(s1 ... sn) nil)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 32
![Page 229: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/229.jpg)
EVAL vs. APPLY
I APPLY does for functions what EVAL does for forms.
I APPLY takes a function name, a list of arguments, and acontext; andapplies the function to the arguments (using context asneeded).
I EVAL: form + context ; s-exprAPPLY: function + args + context ; s-expr
(f s1 ... sn) ⇔ (APPLY 'f '(s1 ... sn) nil)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 32
![Page 230: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/230.jpg)
EVAL vs. APPLY
I APPLY does for functions what EVAL does for forms.
I APPLY takes a function name, a list of arguments, and acontext; andapplies the function to the arguments (using context asneeded).
I EVAL: form + context ; s-exprAPPLY: function + args + context ; s-expr
(f s1 ... sn) ⇔ (APPLY 'f '(s1 ... sn) nil)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 32
![Page 231: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/231.jpg)
EVAL vs. APPLY
I APPLY does for functions what EVAL does for forms.
I APPLY takes a function name, a list of arguments, and acontext; andapplies the function to the arguments (using context asneeded).
I EVAL: form + context ; s-exprAPPLY: function + args + context ; s-expr
(f s1 ... sn) ⇔ (APPLY 'f '(s1 ... sn) nil)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 32
![Page 232: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/232.jpg)
Examples of APPLY
(APPLY 'CONS '(A (B C)) nil)→
(A B C)(APPLY 'CONS '(X (C D E)) '((X .~P)) )→ (X C D E)(APPLY '(LAMBDA (a b) (CONS X b)
'(Y (C D E)) '((X P)) )→ (P C D E)(APPLY 'APPEND '((A B)(C D E)) '((X P)) )→ (A B C D E)}(APPLY '(LAMBDA (x y) (EQ x y))
'(A B) nil ) → nil(APPLY '(LAMBDA (x y) (EQ x y))
'(A B) '((x B)(y B)) ) → nil(APPLY '(LAMBDA (x) (CONS (CAR x) w))
'((A B C))'((x (D E F))(w (G H I))) )
→ (A G H I)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 33
![Page 233: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/233.jpg)
Examples of APPLY
(APPLY 'CONS '(A (B C)) nil)→ (A B C)
(APPLY 'CONS '(X (C D E)) '((X .~P)) )→ (X C D E)(APPLY '(LAMBDA (a b) (CONS X b)
'(Y (C D E)) '((X P)) )→ (P C D E)(APPLY 'APPEND '((A B)(C D E)) '((X P)) )→ (A B C D E)}(APPLY '(LAMBDA (x y) (EQ x y))
'(A B) nil ) → nil(APPLY '(LAMBDA (x y) (EQ x y))
'(A B) '((x B)(y B)) ) → nil(APPLY '(LAMBDA (x) (CONS (CAR x) w))
'((A B C))'((x (D E F))(w (G H I))) )
→ (A G H I)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 33
![Page 234: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/234.jpg)
Examples of APPLY
(APPLY 'CONS '(A (B C)) nil)→ (A B C)(APPLY 'CONS '(X (C D E)) '((X .~P)) )→
(X C D E)(APPLY '(LAMBDA (a b) (CONS X b)
'(Y (C D E)) '((X P)) )→ (P C D E)(APPLY 'APPEND '((A B)(C D E)) '((X P)) )→ (A B C D E)}(APPLY '(LAMBDA (x y) (EQ x y))
'(A B) nil ) → nil(APPLY '(LAMBDA (x y) (EQ x y))
'(A B) '((x B)(y B)) ) → nil(APPLY '(LAMBDA (x) (CONS (CAR x) w))
'((A B C))'((x (D E F))(w (G H I))) )
→ (A G H I)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 33
![Page 235: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/235.jpg)
Examples of APPLY
(APPLY 'CONS '(A (B C)) nil)→ (A B C)(APPLY 'CONS '(X (C D E)) '((X .~P)) )→ (X C D E)
(APPLY '(LAMBDA (a b) (CONS X b)'(Y (C D E)) '((X P)) )
→ (P C D E)(APPLY 'APPEND '((A B)(C D E)) '((X P)) )→ (A B C D E)}(APPLY '(LAMBDA (x y) (EQ x y))
'(A B) nil ) → nil(APPLY '(LAMBDA (x y) (EQ x y))
'(A B) '((x B)(y B)) ) → nil(APPLY '(LAMBDA (x) (CONS (CAR x) w))
'((A B C))'((x (D E F))(w (G H I))) )
→ (A G H I)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 33
![Page 236: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/236.jpg)
Examples of APPLY
(APPLY 'CONS '(A (B C)) nil)→ (A B C)(APPLY 'CONS '(X (C D E)) '((X .~P)) )→ (X C D E)(APPLY '(LAMBDA (a b) (CONS X b)
'(Y (C D E)) '((X P)) )→
(P C D E)(APPLY 'APPEND '((A B)(C D E)) '((X P)) )→ (A B C D E)}(APPLY '(LAMBDA (x y) (EQ x y))
'(A B) nil ) → nil(APPLY '(LAMBDA (x y) (EQ x y))
'(A B) '((x B)(y B)) ) → nil(APPLY '(LAMBDA (x) (CONS (CAR x) w))
'((A B C))'((x (D E F))(w (G H I))) )
→ (A G H I)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 33
![Page 237: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/237.jpg)
Examples of APPLY
(APPLY 'CONS '(A (B C)) nil)→ (A B C)(APPLY 'CONS '(X (C D E)) '((X .~P)) )→ (X C D E)(APPLY '(LAMBDA (a b) (CONS X b)
'(Y (C D E)) '((X P)) )→ (P C D E)
(APPLY 'APPEND '((A B)(C D E)) '((X P)) )→ (A B C D E)}(APPLY '(LAMBDA (x y) (EQ x y))
'(A B) nil ) → nil(APPLY '(LAMBDA (x y) (EQ x y))
'(A B) '((x B)(y B)) ) → nil(APPLY '(LAMBDA (x) (CONS (CAR x) w))
'((A B C))'((x (D E F))(w (G H I))) )
→ (A G H I)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 33
![Page 238: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/238.jpg)
Examples of APPLY
(APPLY 'CONS '(A (B C)) nil)→ (A B C)(APPLY 'CONS '(X (C D E)) '((X .~P)) )→ (X C D E)(APPLY '(LAMBDA (a b) (CONS X b)
'(Y (C D E)) '((X P)) )→ (P C D E)(APPLY 'APPEND '((A B)(C D E)) '((X P)) )→
(A B C D E)}(APPLY '(LAMBDA (x y) (EQ x y))
'(A B) nil ) → nil(APPLY '(LAMBDA (x y) (EQ x y))
'(A B) '((x B)(y B)) ) → nil(APPLY '(LAMBDA (x) (CONS (CAR x) w))
'((A B C))'((x (D E F))(w (G H I))) )
→ (A G H I)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 33
![Page 239: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/239.jpg)
Examples of APPLY
(APPLY 'CONS '(A (B C)) nil)→ (A B C)(APPLY 'CONS '(X (C D E)) '((X .~P)) )→ (X C D E)(APPLY '(LAMBDA (a b) (CONS X b)
'(Y (C D E)) '((X P)) )→ (P C D E)(APPLY 'APPEND '((A B)(C D E)) '((X P)) )→ (A B C D E)}
(APPLY '(LAMBDA (x y) (EQ x y))'(A B) nil ) → nil
(APPLY '(LAMBDA (x y) (EQ x y))'(A B) '((x B)(y B)) ) → nil
(APPLY '(LAMBDA (x) (CONS (CAR x) w))'((A B C))'((x (D E F))(w (G H I))) )
→ (A G H I)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 33
![Page 240: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/240.jpg)
Examples of APPLY
(APPLY 'CONS '(A (B C)) nil)→ (A B C)(APPLY 'CONS '(X (C D E)) '((X .~P)) )→ (X C D E)(APPLY '(LAMBDA (a b) (CONS X b)
'(Y (C D E)) '((X P)) )→ (P C D E)(APPLY 'APPEND '((A B)(C D E)) '((X P)) )→ (A B C D E)}(APPLY '(LAMBDA (x y) (EQ x y))
'(A B) nil ) →
nil(APPLY '(LAMBDA (x y) (EQ x y))
'(A B) '((x B)(y B)) ) → nil(APPLY '(LAMBDA (x) (CONS (CAR x) w))
'((A B C))'((x (D E F))(w (G H I))) )
→ (A G H I)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 33
![Page 241: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/241.jpg)
Examples of APPLY
(APPLY 'CONS '(A (B C)) nil)→ (A B C)(APPLY 'CONS '(X (C D E)) '((X .~P)) )→ (X C D E)(APPLY '(LAMBDA (a b) (CONS X b)
'(Y (C D E)) '((X P)) )→ (P C D E)(APPLY 'APPEND '((A B)(C D E)) '((X P)) )→ (A B C D E)}(APPLY '(LAMBDA (x y) (EQ x y))
'(A B) nil ) → nil
(APPLY '(LAMBDA (x y) (EQ x y))'(A B) '((x B)(y B)) ) → nil
(APPLY '(LAMBDA (x) (CONS (CAR x) w))'((A B C))'((x (D E F))(w (G H I))) )
→ (A G H I)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 33
![Page 242: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/242.jpg)
Examples of APPLY
(APPLY 'CONS '(A (B C)) nil)→ (A B C)(APPLY 'CONS '(X (C D E)) '((X .~P)) )→ (X C D E)(APPLY '(LAMBDA (a b) (CONS X b)
'(Y (C D E)) '((X P)) )→ (P C D E)(APPLY 'APPEND '((A B)(C D E)) '((X P)) )→ (A B C D E)}(APPLY '(LAMBDA (x y) (EQ x y))
'(A B) nil ) → nil(APPLY '(LAMBDA (x y) (EQ x y))
'(A B) '((x B)(y B)) ) →
nil(APPLY '(LAMBDA (x) (CONS (CAR x) w))
'((A B C))'((x (D E F))(w (G H I))) )
→ (A G H I)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 33
![Page 243: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/243.jpg)
Examples of APPLY
(APPLY 'CONS '(A (B C)) nil)→ (A B C)(APPLY 'CONS '(X (C D E)) '((X .~P)) )→ (X C D E)(APPLY '(LAMBDA (a b) (CONS X b)
'(Y (C D E)) '((X P)) )→ (P C D E)(APPLY 'APPEND '((A B)(C D E)) '((X P)) )→ (A B C D E)}(APPLY '(LAMBDA (x y) (EQ x y))
'(A B) nil ) → nil(APPLY '(LAMBDA (x y) (EQ x y))
'(A B) '((x B)(y B)) ) → nil
(APPLY '(LAMBDA (x) (CONS (CAR x) w))'((A B C))'((x (D E F))(w (G H I))) )
→ (A G H I)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 33
![Page 244: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/244.jpg)
Examples of APPLY
(APPLY 'CONS '(A (B C)) nil)→ (A B C)(APPLY 'CONS '(X (C D E)) '((X .~P)) )→ (X C D E)(APPLY '(LAMBDA (a b) (CONS X b)
'(Y (C D E)) '((X P)) )→ (P C D E)(APPLY 'APPEND '((A B)(C D E)) '((X P)) )→ (A B C D E)}(APPLY '(LAMBDA (x y) (EQ x y))
'(A B) nil ) → nil(APPLY '(LAMBDA (x y) (EQ x y))
'(A B) '((x B)(y B)) ) → nil(APPLY '(LAMBDA (x) (CONS (CAR x) w))
'((A B C))'((x (D E F))(w (G H I))) )
→
(A G H I)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 33
![Page 245: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/245.jpg)
Examples of APPLY
(APPLY 'CONS '(A (B C)) nil)→ (A B C)(APPLY 'CONS '(X (C D E)) '((X .~P)) )→ (X C D E)(APPLY '(LAMBDA (a b) (CONS X b)
'(Y (C D E)) '((X P)) )→ (P C D E)(APPLY 'APPEND '((A B)(C D E)) '((X P)) )→ (A B C D E)}(APPLY '(LAMBDA (x y) (EQ x y))
'(A B) nil ) → nil(APPLY '(LAMBDA (x y) (EQ x y))
'(A B) '((x B)(y B)) ) → nil(APPLY '(LAMBDA (x) (CONS (CAR x) w))
'((A B C))'((x (D E F))(w (G H I))) )
→ (A G H I)B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 33
![Page 246: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/246.jpg)
Examples of APPLY II
( (LAMBDA (a b) (APPLY 'EQ (LIST a b) nil))t t)
→
t
( (LAMBDA (x) (APPLY '(LAMBDA () (NULL nil))()'((x T)) ) )
nil)→ t
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 34
![Page 247: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/247.jpg)
Examples of APPLY II
( (LAMBDA (a b) (APPLY 'EQ (LIST a b) nil))t t)
→ t
( (LAMBDA (x) (APPLY '(LAMBDA () (NULL nil))()'((x T)) ) )
nil)→ t
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 34
![Page 248: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/248.jpg)
Examples of APPLY II
( (LAMBDA (a b) (APPLY 'EQ (LIST a b) nil))t t)
→ t
( (LAMBDA (x) (APPLY '(LAMBDA () (NULL nil))()'((x T)) ) )
nil)→
t
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 34
![Page 249: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/249.jpg)
Examples of APPLY II
( (LAMBDA (a b) (APPLY 'EQ (LIST a b) nil))t t)
→ t
( (LAMBDA (x) (APPLY '(LAMBDA () (NULL nil))()'((x T)) ) )
nil)→ t
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 34
![Page 250: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/250.jpg)
Examples of APPLY III
( (LAMBDA (x)(APPLY
'(LAMBDA () (ATOM x))() () ) )
nil)→
t( (LAMBDA (x)
(APPLY '(LAMBDA (y) (EQ x y))'(T) '((x T)) ) )
nil)→t
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 35
![Page 251: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/251.jpg)
Examples of APPLY III
( (LAMBDA (x)(APPLY
'(LAMBDA () (ATOM x))() () ) )
nil)→t
( (LAMBDA (x)(APPLY '(LAMBDA (y) (EQ x y))'(T) '((x T)) ) )
nil)→t
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 35
![Page 252: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/252.jpg)
Examples of APPLY III
( (LAMBDA (x)(APPLY
'(LAMBDA () (ATOM x))() () ) )
nil)→t( (LAMBDA (x)
(APPLY '(LAMBDA (y) (EQ x y))'(T) '((x T)) ) )
nil)→
t
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 35
![Page 253: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/253.jpg)
Examples of APPLY III
( (LAMBDA (x)(APPLY
'(LAMBDA () (ATOM x))() () ) )
nil)→t( (LAMBDA (x)
(APPLY '(LAMBDA (y) (EQ x y))'(T) '((x T)) ) )
nil)→t
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 35
![Page 254: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/254.jpg)
Examples of APPLY IV
( (LAMBDA (x)(APPLY (FUNCTION
(LAMBDA (y) (EQ x y)))'(T) '((x T)) ) )
nil)→
nil
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 36
![Page 255: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/255.jpg)
Examples of APPLY IV
( (LAMBDA (x)(APPLY (FUNCTION
(LAMBDA (y) (EQ x y)))'(T) '((x T)) ) )
nil)→nil
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 36
![Page 256: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/256.jpg)
Application of APPLY to Object Oriented Programming
I "Top Level" Operations:
I (Add 17 22) Integer AdditionI (Add 22.3 -4.2E-1) Real AdditionI (Add (2 . 3) (9 . -7)) Complex Additionfor [2 + 3i] + [9 - 7i]I ...
I Same 'Add' operation, but di�erent (Low level) code
I Other datatypes ? eg Matrix, Group, . . .
I Other operations ? eg Times, LessThan, . . .
I Problem: System must determine appropriate Code,dependenton DATA-Types of Args
I Solution: . . .
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 37
![Page 257: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/257.jpg)
Application of APPLY to Object Oriented Programming
I "Top Level" Operations:I (Add 17 22) Integer Addition
I (Add 22.3 -4.2E-1) Real AdditionI (Add (2 . 3) (9 . -7)) Complex Additionfor [2 + 3i] + [9 - 7i]I ...
I Same 'Add' operation, but di�erent (Low level) code
I Other datatypes ? eg Matrix, Group, . . .
I Other operations ? eg Times, LessThan, . . .
I Problem: System must determine appropriate Code,dependenton DATA-Types of Args
I Solution: . . .
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 37
![Page 258: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/258.jpg)
Application of APPLY to Object Oriented Programming
I "Top Level" Operations:I (Add 17 22) Integer AdditionI (Add 22.3 -4.2E-1) Real Addition
I (Add (2 . 3) (9 . -7)) Complex Additionfor [2 + 3i] + [9 - 7i]I ...
I Same 'Add' operation, but di�erent (Low level) code
I Other datatypes ? eg Matrix, Group, . . .
I Other operations ? eg Times, LessThan, . . .
I Problem: System must determine appropriate Code,dependenton DATA-Types of Args
I Solution: . . .
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 37
![Page 259: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/259.jpg)
Application of APPLY to Object Oriented Programming
I "Top Level" Operations:I (Add 17 22) Integer AdditionI (Add 22.3 -4.2E-1) Real AdditionI (Add (2 . 3) (9 . -7)) Complex Additionfor [2 + 3i] + [9 - 7i]
I ...
I Same 'Add' operation, but di�erent (Low level) code
I Other datatypes ? eg Matrix, Group, . . .
I Other operations ? eg Times, LessThan, . . .
I Problem: System must determine appropriate Code,dependenton DATA-Types of Args
I Solution: . . .
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 37
![Page 260: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/260.jpg)
Application of APPLY to Object Oriented Programming
I "Top Level" Operations:I (Add 17 22) Integer AdditionI (Add 22.3 -4.2E-1) Real AdditionI (Add (2 . 3) (9 . -7)) Complex Additionfor [2 + 3i] + [9 - 7i]I ...
I Same 'Add' operation, but di�erent (Low level) code
I Other datatypes ? eg Matrix, Group, . . .
I Other operations ? eg Times, LessThan, . . .
I Problem: System must determine appropriate Code,dependenton DATA-Types of Args
I Solution: . . .
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 37
![Page 261: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/261.jpg)
Application of APPLY to Object Oriented Programming
I "Top Level" Operations:I (Add 17 22) Integer AdditionI (Add 22.3 -4.2E-1) Real AdditionI (Add (2 . 3) (9 . -7)) Complex Additionfor [2 + 3i] + [9 - 7i]I ...
I Same 'Add' operation, but di�erent (Low level) code
I Other datatypes ? eg Matrix, Group, . . .
I Other operations ? eg Times, LessThan, . . .
I Problem: System must determine appropriate Code,dependenton DATA-Types of Args
I Solution: . . .
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 37
![Page 262: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/262.jpg)
Application of APPLY to Object Oriented Programming
I "Top Level" Operations:I (Add 17 22) Integer AdditionI (Add 22.3 -4.2E-1) Real AdditionI (Add (2 . 3) (9 . -7)) Complex Additionfor [2 + 3i] + [9 - 7i]I ...
I Same 'Add' operation, but di�erent (Low level) code
I Other datatypes ? eg Matrix, Group, . . .
I Other operations ? eg Times, LessThan, . . .
I Problem: System must determine appropriate Code,dependenton DATA-Types of Args
I Solution: . . .
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 37
![Page 263: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/263.jpg)
Application of APPLY to Object Oriented Programming
I "Top Level" Operations:I (Add 17 22) Integer AdditionI (Add 22.3 -4.2E-1) Real AdditionI (Add (2 . 3) (9 . -7)) Complex Additionfor [2 + 3i] + [9 - 7i]I ...
I Same 'Add' operation, but di�erent (Low level) code
I Other datatypes ? eg Matrix, Group, . . .
I Other operations ? eg Times, LessThan, . . .
I Problem: System must determine appropriate Code,dependenton DATA-Types of Args
I Solution: . . .
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 37
![Page 264: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/264.jpg)
Application of APPLY to Object Oriented Programming
I "Top Level" Operations:I (Add 17 22) Integer AdditionI (Add 22.3 -4.2E-1) Real AdditionI (Add (2 . 3) (9 . -7)) Complex Additionfor [2 + 3i] + [9 - 7i]I ...
I Same 'Add' operation, but di�erent (Low level) code
I Other datatypes ? eg Matrix, Group, . . .
I Other operations ? eg Times, LessThan, . . .
I Problem: System must determine appropriate Code,dependenton DATA-Types of Args
I Solution: . . .
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 37
![Page 265: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/265.jpg)
Application of APPLY to Object Oriented Programming
I "Top Level" Operations:I (Add 17 22) Integer AdditionI (Add 22.3 -4.2E-1) Real AdditionI (Add (2 . 3) (9 . -7)) Complex Additionfor [2 + 3i] + [9 - 7i]I ...
I Same 'Add' operation, but di�erent (Low level) code
I Other datatypes ? eg Matrix, Group, . . .
I Other operations ? eg Times, LessThan, . . .
I Problem: System must determine appropriate Code,dependenton DATA-Types of Args
I Solution: . . .
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 37
![Page 266: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/266.jpg)
DataType with Associated Operations
I Integers and Reals:Addition (LAMBDA (x y) (+ x y))Less-Than (LAMBDA (x y) (< x y))
I Complex-Num:Addition (LAMBDA (x y) (CONS (+ (FIRST x) (FIRST y))
(+ (SECOND x) (SECOND y))))Less-Than (LAMBDA (x y) (AND (< (FIRST x) (FIRST y))
(< (SECOND x) (SECOND y))))
I Matrix:Addition (LAMBDA (x y) . . . )Less-Than (LAMBDA (x y) . . . )
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 38
![Page 267: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/267.jpg)
DataType with Associated Operations
I Integers and Reals:Addition (LAMBDA (x y) (+ x y))Less-Than (LAMBDA (x y) (< x y))
I Complex-Num:Addition (LAMBDA (x y) (CONS (+ (FIRST x) (FIRST y))
(+ (SECOND x) (SECOND y))))Less-Than (LAMBDA (x y) (AND (< (FIRST x) (FIRST y))
(< (SECOND x) (SECOND y))))
I Matrix:Addition (LAMBDA (x y) . . . )Less-Than (LAMBDA (x y) . . . )
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 38
![Page 268: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/268.jpg)
DataType with Associated Operations
I Integers and Reals:Addition (LAMBDA (x y) (+ x y))Less-Than (LAMBDA (x y) (< x y))
I Complex-Num:Addition (LAMBDA (x y) (CONS (+ (FIRST x) (FIRST y))
(+ (SECOND x) (SECOND y))))Less-Than (LAMBDA (x y) (AND (< (FIRST x) (FIRST y))
(< (SECOND x) (SECOND y))))
I Matrix:Addition (LAMBDA (x y) . . . )Less-Than (LAMBDA (x y) . . . )
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 38
![Page 269: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/269.jpg)
Object-Oriented Programming II
I Code for add:
(DEFUN add (x y)(APPLY
(Find-Addition-Method x y) ;; implementation(LIST x y) ;; argument listnil)) ;; context
I Find-Addition-Method
1. Determine �data type� of args[�Real� for args 22.3, -15.2]
2. Find method for that operationfor that data types(using default, inheritance, . . . )[�(LAMBDA (x y) (+ x y))� for Real Addition]
I Similar function for Times, LessThan, . . .
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 39
![Page 270: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/270.jpg)
Object-Oriented Programming II
I Code for add:(DEFUN add (x y)
(APPLY(Find-Addition-Method x y) ;; implementation(LIST x y) ;; argument listnil)) ;; context
I Find-Addition-Method
1. Determine �data type� of args[�Real� for args 22.3, -15.2]
2. Find method for that operationfor that data types(using default, inheritance, . . . )[�(LAMBDA (x y) (+ x y))� for Real Addition]
I Similar function for Times, LessThan, . . .
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 39
![Page 271: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/271.jpg)
Object-Oriented Programming II
I Code for add:(DEFUN add (x y)
(APPLY(Find-Addition-Method x y) ;; implementation(LIST x y) ;; argument listnil)) ;; context
I Find-Addition-Method
1. Determine �data type� of args[�Real� for args 22.3, -15.2]
2. Find method for that operationfor that data types(using default, inheritance, . . . )[�(LAMBDA (x y) (+ x y))� for Real Addition]
I Similar function for Times, LessThan, . . .
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 39
![Page 272: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/272.jpg)
Object-Oriented Programming II
I Code for add:(DEFUN add (x y)
(APPLY(Find-Addition-Method x y) ;; implementation(LIST x y) ;; argument listnil)) ;; context
I Find-Addition-Method1. Determine �data type� of args
[�Real� for args 22.3, -15.2]
2. Find method for that operationfor that data types(using default, inheritance, . . . )[�(LAMBDA (x y) (+ x y))� for Real Addition]
I Similar function for Times, LessThan, . . .
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 39
![Page 273: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/273.jpg)
Object-Oriented Programming II
I Code for add:(DEFUN add (x y)
(APPLY(Find-Addition-Method x y) ;; implementation(LIST x y) ;; argument listnil)) ;; context
I Find-Addition-Method1. Determine �data type� of args
[�Real� for args 22.3, -15.2]2. Find method for that operation
for that data types(using default, inheritance, . . . )[�(LAMBDA (x y) (+ x y))� for Real Addition]
I Similar function for Times, LessThan, . . .
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 39
![Page 274: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/274.jpg)
Object-Oriented Programming II
I Code for add:(DEFUN add (x y)
(APPLY(Find-Addition-Method x y) ;; implementation(LIST x y) ;; argument listnil)) ;; context
I Find-Addition-Method1. Determine �data type� of args
[�Real� for args 22.3, -15.2]2. Find method for that operation
for that data types(using default, inheritance, . . . )[�(LAMBDA (x y) (+ x y))� for Real Addition]
I Similar function for Times, LessThan, . . .B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 39
![Page 275: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/275.jpg)
APPLY Summary
I Apply is de�ned in Common Lisp(Context 6= alist)
I It can take (only) 2 args:FunctionList of arguments(Context taken to be nil)
I Also Funcall:Like Apply, but takes n + 1 args:First is function;i + 1st is i th arg to function.
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 40
![Page 276: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/276.jpg)
APPLY Summary
I Apply is de�ned in Common Lisp(Context 6= alist)
I It can take (only) 2 args:FunctionList of arguments(Context taken to be nil)
I Also Funcall:Like Apply, but takes n + 1 args:First is function;i + 1st is i th arg to function.
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 40
![Page 277: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/277.jpg)
APPLY Summary
I Apply is de�ned in Common Lisp(Context 6= alist)
I It can take (only) 2 args:FunctionList of arguments(Context taken to be nil)
I Also Funcall:Like Apply, but takes n + 1 args:First is function;i + 1st is i th arg to function.
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 40
![Page 278: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/278.jpg)
More Examples of Apply
(apply '+ (3 5)) →
8(funcall '+ 3 5)} → 8(apply 'car '((a b c))) → a(funcall 'car '(a b c))→ a(apply '(lambda (x) (cadr x)) '((a b c))) →
b
(funcall '(lambda (x) (cadr x)) '(a b c)) →b(apply '(lambda (x y) (eq x y)) '(a b))→ nil(funcall '(lambda (x y) (eq x y)) 'a 'b) → nil
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 41
![Page 279: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/279.jpg)
More Examples of Apply
(apply '+ (3 5)) → 8
(funcall '+ 3 5)} → 8(apply 'car '((a b c))) → a(funcall 'car '(a b c))→ a(apply '(lambda (x) (cadr x)) '((a b c))) →
b
(funcall '(lambda (x) (cadr x)) '(a b c)) →b(apply '(lambda (x y) (eq x y)) '(a b))→ nil(funcall '(lambda (x y) (eq x y)) 'a 'b) → nil
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 41
![Page 280: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/280.jpg)
More Examples of Apply
(apply '+ (3 5)) → 8(funcall '+ 3 5)} →
8(apply 'car '((a b c))) → a(funcall 'car '(a b c))→ a(apply '(lambda (x) (cadr x)) '((a b c))) →
b
(funcall '(lambda (x) (cadr x)) '(a b c)) →b(apply '(lambda (x y) (eq x y)) '(a b))→ nil(funcall '(lambda (x y) (eq x y)) 'a 'b) → nil
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 41
![Page 281: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/281.jpg)
More Examples of Apply
(apply '+ (3 5)) → 8(funcall '+ 3 5)} → 8
(apply 'car '((a b c))) → a(funcall 'car '(a b c))→ a(apply '(lambda (x) (cadr x)) '((a b c))) →
b
(funcall '(lambda (x) (cadr x)) '(a b c)) →b(apply '(lambda (x y) (eq x y)) '(a b))→ nil(funcall '(lambda (x y) (eq x y)) 'a 'b) → nil
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 41
![Page 282: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/282.jpg)
More Examples of Apply
(apply '+ (3 5)) → 8(funcall '+ 3 5)} → 8(apply 'car '((a b c))) →
a(funcall 'car '(a b c))→ a(apply '(lambda (x) (cadr x)) '((a b c))) →
b
(funcall '(lambda (x) (cadr x)) '(a b c)) →b(apply '(lambda (x y) (eq x y)) '(a b))→ nil(funcall '(lambda (x y) (eq x y)) 'a 'b) → nil
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 41
![Page 283: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/283.jpg)
More Examples of Apply
(apply '+ (3 5)) → 8(funcall '+ 3 5)} → 8(apply 'car '((a b c))) → a
(funcall 'car '(a b c))→ a(apply '(lambda (x) (cadr x)) '((a b c))) →
b
(funcall '(lambda (x) (cadr x)) '(a b c)) →b(apply '(lambda (x y) (eq x y)) '(a b))→ nil(funcall '(lambda (x y) (eq x y)) 'a 'b) → nil
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 41
![Page 284: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/284.jpg)
More Examples of Apply
(apply '+ (3 5)) → 8(funcall '+ 3 5)} → 8(apply 'car '((a b c))) → a(funcall 'car '(a b c))→
a(apply '(lambda (x) (cadr x)) '((a b c))) →
b
(funcall '(lambda (x) (cadr x)) '(a b c)) →b(apply '(lambda (x y) (eq x y)) '(a b))→ nil(funcall '(lambda (x y) (eq x y)) 'a 'b) → nil
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 41
![Page 285: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/285.jpg)
More Examples of Apply
(apply '+ (3 5)) → 8(funcall '+ 3 5)} → 8(apply 'car '((a b c))) → a(funcall 'car '(a b c))→ a
(apply '(lambda (x) (cadr x)) '((a b c))) →
b
(funcall '(lambda (x) (cadr x)) '(a b c)) →b(apply '(lambda (x y) (eq x y)) '(a b))→ nil(funcall '(lambda (x y) (eq x y)) 'a 'b) → nil
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 41
![Page 286: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/286.jpg)
More Examples of Apply
(apply '+ (3 5)) → 8(funcall '+ 3 5)} → 8(apply 'car '((a b c))) → a(funcall 'car '(a b c))→ a(apply '(lambda (x) (cadr x)) '((a b c))) → b
(funcall '(lambda (x) (cadr x)) '(a b c)) →b(apply '(lambda (x y) (eq x y)) '(a b))→ nil(funcall '(lambda (x y) (eq x y)) 'a 'b) → nil
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 41
![Page 287: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/287.jpg)
More Examples of Apply
(apply '+ (3 5)) → 8(funcall '+ 3 5)} → 8(apply 'car '((a b c))) → a(funcall 'car '(a b c))→ a(apply '(lambda (x) (cadr x)) '((a b c))) → b
(funcall '(lambda (x) (cadr x)) '(a b c)) →b(apply '(lambda (x y) (eq x y)) '(a b))→ nil(funcall '(lambda (x y) (eq x y)) 'a 'b) → nil
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 41
![Page 288: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/288.jpg)
More Examples of Apply
(apply '+ (3 5)) → 8(funcall '+ 3 5)} → 8(apply 'car '((a b c))) → a(funcall 'car '(a b c))→ a(apply '(lambda (x) (cadr x)) '((a b c))) → b(funcall '(lambda (x) (cadr x)) '(a b c)) →
b(apply '(lambda (x y) (eq x y)) '(a b))→ nil(funcall '(lambda (x y) (eq x y)) 'a 'b) → nil
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 41
![Page 289: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/289.jpg)
More Examples of Apply
(apply '+ (3 5)) → 8(funcall '+ 3 5)} → 8(apply 'car '((a b c))) → a(funcall 'car '(a b c))→ a(apply '(lambda (x) (cadr x)) '((a b c))) → b(funcall '(lambda (x) (cadr x)) '(a b c)) →b
(apply '(lambda (x y) (eq x y)) '(a b))→ nil(funcall '(lambda (x y) (eq x y)) 'a 'b) → nil
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 41
![Page 290: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/290.jpg)
More Examples of Apply
(apply '+ (3 5)) → 8(funcall '+ 3 5)} → 8(apply 'car '((a b c))) → a(funcall 'car '(a b c))→ a(apply '(lambda (x) (cadr x)) '((a b c))) → b(funcall '(lambda (x) (cadr x)) '(a b c)) →b(apply '(lambda (x y) (eq x y)) '(a b))→
nil(funcall '(lambda (x y) (eq x y)) 'a 'b) → nil
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 41
![Page 291: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/291.jpg)
More Examples of Apply
(apply '+ (3 5)) → 8(funcall '+ 3 5)} → 8(apply 'car '((a b c))) → a(funcall 'car '(a b c))→ a(apply '(lambda (x) (cadr x)) '((a b c))) → b(funcall '(lambda (x) (cadr x)) '(a b c)) →b(apply '(lambda (x y) (eq x y)) '(a b))→ nil
(funcall '(lambda (x y) (eq x y)) 'a 'b) → nil
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 41
![Page 292: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/292.jpg)
More Examples of Apply
(apply '+ (3 5)) → 8(funcall '+ 3 5)} → 8(apply 'car '((a b c))) → a(funcall 'car '(a b c))→ a(apply '(lambda (x) (cadr x)) '((a b c))) → b(funcall '(lambda (x) (cadr x)) '(a b c)) →b(apply '(lambda (x y) (eq x y)) '(a b))→ nil(funcall '(lambda (x y) (eq x y)) 'a 'b) →
nil
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 41
![Page 293: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/293.jpg)
More Examples of Apply
(apply '+ (3 5)) → 8(funcall '+ 3 5)} → 8(apply 'car '((a b c))) → a(funcall 'car '(a b c))→ a(apply '(lambda (x) (cadr x)) '((a b c))) → b(funcall '(lambda (x) (cadr x)) '(a b c)) →b(apply '(lambda (x y) (eq x y)) '(a b))→ nil(funcall '(lambda (x y) (eq x y)) 'a 'b) → nil
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 41
![Page 294: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/294.jpg)
And for your amusement
I What does this code do?
( (lambda (arg)(list arg(list (quote quote) arg)) )
(quote(lambda (arg)(list arg
(list (quote quote) arg)))) )
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 42
![Page 295: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/295.jpg)
Lazy Computation
I Usually being "lazy" is bad
I When might it be good?
I Unsure if compuation is necesaryI When a computation might never halt
I Common Lisp does not directly support laziness (otherlanguages do)
I Easy to add (but �rst, some examples)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 43
![Page 296: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/296.jpg)
Lazy Computation
I Usually being "lazy" is bad
I When might it be good?
I Unsure if compuation is necesaryI When a computation might never halt
I Common Lisp does not directly support laziness (otherlanguages do)
I Easy to add (but �rst, some examples)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 43
![Page 297: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/297.jpg)
Lazy Computation
I Usually being "lazy" is bad
I When might it be good?I Unsure if compuation is necesary
I When a computation might never halt
I Common Lisp does not directly support laziness (otherlanguages do)
I Easy to add (but �rst, some examples)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 43
![Page 298: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/298.jpg)
Lazy Computation
I Usually being "lazy" is bad
I When might it be good?I Unsure if compuation is necesaryI When a computation might never halt
I Common Lisp does not directly support laziness (otherlanguages do)
I Easy to add (but �rst, some examples)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 43
![Page 299: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/299.jpg)
Lazy Computation
I Usually being "lazy" is bad
I When might it be good?I Unsure if compuation is necesaryI When a computation might never halt
I Common Lisp does not directly support laziness (otherlanguages do)
I Easy to add (but �rst, some examples)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 43
![Page 300: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/300.jpg)
Lazy Computation
I Usually being "lazy" is bad
I When might it be good?I Unsure if compuation is necesaryI When a computation might never halt
I Common Lisp does not directly support laziness (otherlanguages do)
I Easy to add (but �rst, some examples)
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 43
![Page 301: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/301.jpg)
Lazy Computation
I A typical Lisp calculation
(setf p (+ 2 3)) → 5p → 5
I Lazy calculations are introduced with "delay".
I A delayed computation can be restarted using "force".
I Example (not supported directly by Lisp)
(setf P (delay (+ 2 3))) →"#<DELAYED-COMPUTATION>"(force p) → 5
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 44
![Page 302: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/302.jpg)
Lazy Computation
I A typical Lisp calculation
(setf p (+ 2 3)) → 5p → 5
I Lazy calculations are introduced with "delay".
I A delayed computation can be restarted using "force".
I Example (not supported directly by Lisp)
(setf P (delay (+ 2 3))) →"#<DELAYED-COMPUTATION>"(force p) → 5
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 44
![Page 303: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/303.jpg)
Lazy Computation
I A typical Lisp calculation
(setf p (+ 2 3)) → 5p → 5
I Lazy calculations are introduced with "delay".
I A delayed computation can be restarted using "force".
I Example (not supported directly by Lisp)
(setf P (delay (+ 2 3))) →"#<DELAYED-COMPUTATION>"(force p) → 5
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 44
![Page 304: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/304.jpg)
Lazy Computation
I A typical Lisp calculation
(setf p (+ 2 3)) → 5p → 5
I Lazy calculations are introduced with "delay".
I A delayed computation can be restarted using "force".
I Example (not supported directly by Lisp)
(setf P (delay (+ 2 3))) →
"#<DELAYED-COMPUTATION>"(force p) → 5
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 44
![Page 305: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/305.jpg)
Lazy Computation
I A typical Lisp calculation
(setf p (+ 2 3)) → 5p → 5
I Lazy calculations are introduced with "delay".
I A delayed computation can be restarted using "force".
I Example (not supported directly by Lisp)
(setf P (delay (+ 2 3))) →"#<DELAYED-COMPUTATION>"
(force p) → 5
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 44
![Page 306: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/306.jpg)
Lazy Computation
I A typical Lisp calculation
(setf p (+ 2 3)) → 5p → 5
I Lazy calculations are introduced with "delay".
I A delayed computation can be restarted using "force".
I Example (not supported directly by Lisp)
(setf P (delay (+ 2 3))) →"#<DELAYED-COMPUTATION>"(force p) →
5
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 44
![Page 307: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/307.jpg)
Lazy Computation
I A typical Lisp calculation
(setf p (+ 2 3)) → 5p → 5
I Lazy calculations are introduced with "delay".
I A delayed computation can be restarted using "force".
I Example (not supported directly by Lisp)
(setf P (delay (+ 2 3))) →"#<DELAYED-COMPUTATION>"(force p) → 5
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 44
![Page 308: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/308.jpg)
Lazy List Computation
I Lazy computations work well with recursive data-structures
I De�ne lazy "cons" which delays evaluation of its secondargument
(setf p (lcons a (lcons b nil)))→ (A . "#<DELAYED-COMPUTATION>")(lcar p) → A(lcdr p) → (B . "#<DELAYED-COMPUTATION>")(lcdr (lcdr p)) → ()
(setf q (lcons (+ 2 1) (+ 5 6))) →(3 . "#<DELAYED-COMPUTATION>")(lcar q) → 3(lcdr q) → 11
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 45
![Page 309: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/309.jpg)
Lazy List Computation
I Lazy computations work well with recursive data-structures
I De�ne lazy "cons" which delays evaluation of its secondargument
(setf p (lcons a (lcons b nil)))→ (A . "#<DELAYED-COMPUTATION>")
(lcar p) → A(lcdr p) → (B . "#<DELAYED-COMPUTATION>")(lcdr (lcdr p)) → ()
(setf q (lcons (+ 2 1) (+ 5 6))) →(3 . "#<DELAYED-COMPUTATION>")(lcar q) → 3(lcdr q) → 11
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 45
![Page 310: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/310.jpg)
Lazy List Computation
I Lazy computations work well with recursive data-structures
I De�ne lazy "cons" which delays evaluation of its secondargument
(setf p (lcons a (lcons b nil)))→ (A . "#<DELAYED-COMPUTATION>")(lcar p) →
A(lcdr p) → (B . "#<DELAYED-COMPUTATION>")(lcdr (lcdr p)) → ()
(setf q (lcons (+ 2 1) (+ 5 6))) →(3 . "#<DELAYED-COMPUTATION>")(lcar q) → 3(lcdr q) → 11
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 45
![Page 311: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/311.jpg)
Lazy List Computation
I Lazy computations work well with recursive data-structures
I De�ne lazy "cons" which delays evaluation of its secondargument
(setf p (lcons a (lcons b nil)))→ (A . "#<DELAYED-COMPUTATION>")(lcar p) → A
(lcdr p) → (B . "#<DELAYED-COMPUTATION>")(lcdr (lcdr p)) → ()
(setf q (lcons (+ 2 1) (+ 5 6))) →(3 . "#<DELAYED-COMPUTATION>")(lcar q) → 3(lcdr q) → 11
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 45
![Page 312: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/312.jpg)
Lazy List Computation
I Lazy computations work well with recursive data-structures
I De�ne lazy "cons" which delays evaluation of its secondargument
(setf p (lcons a (lcons b nil)))→ (A . "#<DELAYED-COMPUTATION>")(lcar p) → A(lcdr p) →
(B . "#<DELAYED-COMPUTATION>")(lcdr (lcdr p)) → ()
(setf q (lcons (+ 2 1) (+ 5 6))) →(3 . "#<DELAYED-COMPUTATION>")(lcar q) → 3(lcdr q) → 11
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 45
![Page 313: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/313.jpg)
Lazy List Computation
I Lazy computations work well with recursive data-structures
I De�ne lazy "cons" which delays evaluation of its secondargument
(setf p (lcons a (lcons b nil)))→ (A . "#<DELAYED-COMPUTATION>")(lcar p) → A(lcdr p) → (B . "#<DELAYED-COMPUTATION>")
(lcdr (lcdr p)) → ()
(setf q (lcons (+ 2 1) (+ 5 6))) →(3 . "#<DELAYED-COMPUTATION>")(lcar q) → 3(lcdr q) → 11
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 45
![Page 314: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/314.jpg)
Lazy List Computation
I Lazy computations work well with recursive data-structures
I De�ne lazy "cons" which delays evaluation of its secondargument
(setf p (lcons a (lcons b nil)))→ (A . "#<DELAYED-COMPUTATION>")(lcar p) → A(lcdr p) → (B . "#<DELAYED-COMPUTATION>")(lcdr (lcdr p)) →
()
(setf q (lcons (+ 2 1) (+ 5 6))) →(3 . "#<DELAYED-COMPUTATION>")(lcar q) → 3(lcdr q) → 11
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 45
![Page 315: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/315.jpg)
Lazy List Computation
I Lazy computations work well with recursive data-structures
I De�ne lazy "cons" which delays evaluation of its secondargument
(setf p (lcons a (lcons b nil)))→ (A . "#<DELAYED-COMPUTATION>")(lcar p) → A(lcdr p) → (B . "#<DELAYED-COMPUTATION>")(lcdr (lcdr p)) → ()
(setf q (lcons (+ 2 1) (+ 5 6))) →(3 . "#<DELAYED-COMPUTATION>")(lcar q) → 3(lcdr q) → 11
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 45
![Page 316: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/316.jpg)
Lazy List Computation
I Lazy computations work well with recursive data-structures
I De�ne lazy "cons" which delays evaluation of its secondargument
(setf p (lcons a (lcons b nil)))→ (A . "#<DELAYED-COMPUTATION>")(lcar p) → A(lcdr p) → (B . "#<DELAYED-COMPUTATION>")(lcdr (lcdr p)) → ()
(setf q (lcons (+ 2 1) (+ 5 6))) →
(3 . "#<DELAYED-COMPUTATION>")(lcar q) → 3(lcdr q) → 11
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 45
![Page 317: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/317.jpg)
Lazy List Computation
I Lazy computations work well with recursive data-structures
I De�ne lazy "cons" which delays evaluation of its secondargument
(setf p (lcons a (lcons b nil)))→ (A . "#<DELAYED-COMPUTATION>")(lcar p) → A(lcdr p) → (B . "#<DELAYED-COMPUTATION>")(lcdr (lcdr p)) → ()
(setf q (lcons (+ 2 1) (+ 5 6))) →(3 . "#<DELAYED-COMPUTATION>")
(lcar q) → 3(lcdr q) → 11
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 45
![Page 318: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/318.jpg)
Lazy List Computation
I Lazy computations work well with recursive data-structures
I De�ne lazy "cons" which delays evaluation of its secondargument
(setf p (lcons a (lcons b nil)))→ (A . "#<DELAYED-COMPUTATION>")(lcar p) → A(lcdr p) → (B . "#<DELAYED-COMPUTATION>")(lcdr (lcdr p)) → ()
(setf q (lcons (+ 2 1) (+ 5 6))) →(3 . "#<DELAYED-COMPUTATION>")(lcar q) →
3(lcdr q) → 11
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 45
![Page 319: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/319.jpg)
Lazy List Computation
I Lazy computations work well with recursive data-structures
I De�ne lazy "cons" which delays evaluation of its secondargument
(setf p (lcons a (lcons b nil)))→ (A . "#<DELAYED-COMPUTATION>")(lcar p) → A(lcdr p) → (B . "#<DELAYED-COMPUTATION>")(lcdr (lcdr p)) → ()
(setf q (lcons (+ 2 1) (+ 5 6))) →(3 . "#<DELAYED-COMPUTATION>")(lcar q) → 3
(lcdr q) → 11
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 45
![Page 320: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/320.jpg)
Lazy List Computation
I Lazy computations work well with recursive data-structures
I De�ne lazy "cons" which delays evaluation of its secondargument
(setf p (lcons a (lcons b nil)))→ (A . "#<DELAYED-COMPUTATION>")(lcar p) → A(lcdr p) → (B . "#<DELAYED-COMPUTATION>")(lcdr (lcdr p)) → ()
(setf q (lcons (+ 2 1) (+ 5 6))) →(3 . "#<DELAYED-COMPUTATION>")(lcar q) → 3(lcdr q) →
11
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 45
![Page 321: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/321.jpg)
Lazy List Computation
I Lazy computations work well with recursive data-structures
I De�ne lazy "cons" which delays evaluation of its secondargument
(setf p (lcons a (lcons b nil)))→ (A . "#<DELAYED-COMPUTATION>")(lcar p) → A(lcdr p) → (B . "#<DELAYED-COMPUTATION>")(lcdr (lcdr p)) → ()
(setf q (lcons (+ 2 1) (+ 5 6))) →(3 . "#<DELAYED-COMPUTATION>")(lcar q) → 3(lcdr q) → 11
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 45
![Page 322: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/322.jpg)
Lazy List Computation
(setf q (lcons (+ 2 1) (setf x 5)))
x → undefined!(lcdr q) → 5x → 5
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 46
![Page 323: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/323.jpg)
Lazy List Computation
(setf q (lcons (+ 2 1) (setf x 5)))x →
undefined!(lcdr q) → 5x → 5
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 46
![Page 324: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/324.jpg)
Lazy List Computation
(setf q (lcons (+ 2 1) (setf x 5)))x → undefined!
(lcdr q) → 5x → 5
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 46
![Page 325: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/325.jpg)
Lazy List Computation
(setf q (lcons (+ 2 1) (setf x 5)))x → undefined!(lcdr q) →
5x → 5
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 46
![Page 326: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/326.jpg)
Lazy List Computation
(setf q (lcons (+ 2 1) (setf x 5)))x → undefined!(lcdr q) → 5
x → 5
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 46
![Page 327: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/327.jpg)
Lazy List Computation
(setf q (lcons (+ 2 1) (setf x 5)))x → undefined!(lcdr q) → 5x →
5
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 46
![Page 328: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/328.jpg)
Lazy List Computation
(setf q (lcons (+ 2 1) (setf x 5)))x → undefined!(lcdr q) → 5x → 5
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 46
![Page 329: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/329.jpg)
In�nite Computations
I What does this recursion compute?
(defun numbers (x)(lcons x (numbers (1+ x))))
(setf p (numbers 0))(lcar p) → 0(lcdr p) → (1 . "#<DELAYED-COMPUTATION>")(lcar (lcdr p)) → 1(lcar (lcdr (lcdr p))) → 2(lcar (lcdr (lcdr (lcdr p)))) → 3(defun fibset (f1 f2)
(lcons f1 (fibset f2 (+ f1 f2))))(setf q (fibset 1 1))(lcar (lcdr (lcdr (lcdr (lcdr q))))) → 5
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 47
![Page 330: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/330.jpg)
In�nite Computations
I What does this recursion compute?
(defun numbers (x)(lcons x (numbers (1+ x))))
(setf p (numbers 0))
(lcar p) → 0(lcdr p) → (1 . "#<DELAYED-COMPUTATION>")(lcar (lcdr p)) → 1(lcar (lcdr (lcdr p))) → 2(lcar (lcdr (lcdr (lcdr p)))) → 3(defun fibset (f1 f2)
(lcons f1 (fibset f2 (+ f1 f2))))(setf q (fibset 1 1))(lcar (lcdr (lcdr (lcdr (lcdr q))))) → 5
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 47
![Page 331: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/331.jpg)
In�nite Computations
I What does this recursion compute?
(defun numbers (x)(lcons x (numbers (1+ x))))
(setf p (numbers 0))(lcar p) →
0(lcdr p) → (1 . "#<DELAYED-COMPUTATION>")(lcar (lcdr p)) → 1(lcar (lcdr (lcdr p))) → 2(lcar (lcdr (lcdr (lcdr p)))) → 3(defun fibset (f1 f2)
(lcons f1 (fibset f2 (+ f1 f2))))(setf q (fibset 1 1))(lcar (lcdr (lcdr (lcdr (lcdr q))))) → 5
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 47
![Page 332: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/332.jpg)
In�nite Computations
I What does this recursion compute?
(defun numbers (x)(lcons x (numbers (1+ x))))
(setf p (numbers 0))(lcar p) → 0
(lcdr p) → (1 . "#<DELAYED-COMPUTATION>")(lcar (lcdr p)) → 1(lcar (lcdr (lcdr p))) → 2(lcar (lcdr (lcdr (lcdr p)))) → 3(defun fibset (f1 f2)
(lcons f1 (fibset f2 (+ f1 f2))))(setf q (fibset 1 1))(lcar (lcdr (lcdr (lcdr (lcdr q))))) → 5
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 47
![Page 333: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/333.jpg)
In�nite Computations
I What does this recursion compute?
(defun numbers (x)(lcons x (numbers (1+ x))))
(setf p (numbers 0))(lcar p) → 0(lcdr p) →
(1 . "#<DELAYED-COMPUTATION>")(lcar (lcdr p)) → 1(lcar (lcdr (lcdr p))) → 2(lcar (lcdr (lcdr (lcdr p)))) → 3(defun fibset (f1 f2)
(lcons f1 (fibset f2 (+ f1 f2))))(setf q (fibset 1 1))(lcar (lcdr (lcdr (lcdr (lcdr q))))) → 5
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 47
![Page 334: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/334.jpg)
In�nite Computations
I What does this recursion compute?
(defun numbers (x)(lcons x (numbers (1+ x))))
(setf p (numbers 0))(lcar p) → 0(lcdr p) → (1 . "#<DELAYED-COMPUTATION>")
(lcar (lcdr p)) → 1(lcar (lcdr (lcdr p))) → 2(lcar (lcdr (lcdr (lcdr p)))) → 3(defun fibset (f1 f2)
(lcons f1 (fibset f2 (+ f1 f2))))(setf q (fibset 1 1))(lcar (lcdr (lcdr (lcdr (lcdr q))))) → 5
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 47
![Page 335: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/335.jpg)
In�nite Computations
I What does this recursion compute?
(defun numbers (x)(lcons x (numbers (1+ x))))
(setf p (numbers 0))(lcar p) → 0(lcdr p) → (1 . "#<DELAYED-COMPUTATION>")(lcar (lcdr p)) →
1(lcar (lcdr (lcdr p))) → 2(lcar (lcdr (lcdr (lcdr p)))) → 3(defun fibset (f1 f2)
(lcons f1 (fibset f2 (+ f1 f2))))(setf q (fibset 1 1))(lcar (lcdr (lcdr (lcdr (lcdr q))))) → 5
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 47
![Page 336: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/336.jpg)
In�nite Computations
I What does this recursion compute?
(defun numbers (x)(lcons x (numbers (1+ x))))
(setf p (numbers 0))(lcar p) → 0(lcdr p) → (1 . "#<DELAYED-COMPUTATION>")(lcar (lcdr p)) → 1
(lcar (lcdr (lcdr p))) → 2(lcar (lcdr (lcdr (lcdr p)))) → 3(defun fibset (f1 f2)
(lcons f1 (fibset f2 (+ f1 f2))))(setf q (fibset 1 1))(lcar (lcdr (lcdr (lcdr (lcdr q))))) → 5
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 47
![Page 337: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/337.jpg)
In�nite Computations
I What does this recursion compute?
(defun numbers (x)(lcons x (numbers (1+ x))))
(setf p (numbers 0))(lcar p) → 0(lcdr p) → (1 . "#<DELAYED-COMPUTATION>")(lcar (lcdr p)) → 1(lcar (lcdr (lcdr p))) →
2(lcar (lcdr (lcdr (lcdr p)))) → 3(defun fibset (f1 f2)
(lcons f1 (fibset f2 (+ f1 f2))))(setf q (fibset 1 1))(lcar (lcdr (lcdr (lcdr (lcdr q))))) → 5
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 47
![Page 338: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/338.jpg)
In�nite Computations
I What does this recursion compute?
(defun numbers (x)(lcons x (numbers (1+ x))))
(setf p (numbers 0))(lcar p) → 0(lcdr p) → (1 . "#<DELAYED-COMPUTATION>")(lcar (lcdr p)) → 1(lcar (lcdr (lcdr p))) → 2
(lcar (lcdr (lcdr (lcdr p)))) → 3(defun fibset (f1 f2)
(lcons f1 (fibset f2 (+ f1 f2))))(setf q (fibset 1 1))(lcar (lcdr (lcdr (lcdr (lcdr q))))) → 5
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 47
![Page 339: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/339.jpg)
In�nite Computations
I What does this recursion compute?
(defun numbers (x)(lcons x (numbers (1+ x))))
(setf p (numbers 0))(lcar p) → 0(lcdr p) → (1 . "#<DELAYED-COMPUTATION>")(lcar (lcdr p)) → 1(lcar (lcdr (lcdr p))) → 2(lcar (lcdr (lcdr (lcdr p)))) →
3(defun fibset (f1 f2)
(lcons f1 (fibset f2 (+ f1 f2))))(setf q (fibset 1 1))(lcar (lcdr (lcdr (lcdr (lcdr q))))) → 5
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 47
![Page 340: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/340.jpg)
In�nite Computations
I What does this recursion compute?
(defun numbers (x)(lcons x (numbers (1+ x))))
(setf p (numbers 0))(lcar p) → 0(lcdr p) → (1 . "#<DELAYED-COMPUTATION>")(lcar (lcdr p)) → 1(lcar (lcdr (lcdr p))) → 2(lcar (lcdr (lcdr (lcdr p)))) → 3
(defun fibset (f1 f2)(lcons f1 (fibset f2 (+ f1 f2))))
(setf q (fibset 1 1))(lcar (lcdr (lcdr (lcdr (lcdr q))))) → 5
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 47
![Page 341: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/341.jpg)
In�nite Computations
I What does this recursion compute?
(defun numbers (x)(lcons x (numbers (1+ x))))
(setf p (numbers 0))(lcar p) → 0(lcdr p) → (1 . "#<DELAYED-COMPUTATION>")(lcar (lcdr p)) → 1(lcar (lcdr (lcdr p))) → 2(lcar (lcdr (lcdr (lcdr p)))) → 3(defun fibset (f1 f2)
(lcons f1 (fibset f2 (+ f1 f2))))
(setf q (fibset 1 1))(lcar (lcdr (lcdr (lcdr (lcdr q))))) → 5
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 47
![Page 342: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/342.jpg)
In�nite Computations
I What does this recursion compute?
(defun numbers (x)(lcons x (numbers (1+ x))))
(setf p (numbers 0))(lcar p) → 0(lcdr p) → (1 . "#<DELAYED-COMPUTATION>")(lcar (lcdr p)) → 1(lcar (lcdr (lcdr p))) → 2(lcar (lcdr (lcdr (lcdr p)))) → 3(defun fibset (f1 f2)
(lcons f1 (fibset f2 (+ f1 f2))))(setf q (fibset 1 1))
(lcar (lcdr (lcdr (lcdr (lcdr q))))) → 5
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 47
![Page 343: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/343.jpg)
In�nite Computations
I What does this recursion compute?
(defun numbers (x)(lcons x (numbers (1+ x))))
(setf p (numbers 0))(lcar p) → 0(lcdr p) → (1 . "#<DELAYED-COMPUTATION>")(lcar (lcdr p)) → 1(lcar (lcdr (lcdr p))) → 2(lcar (lcdr (lcdr (lcdr p)))) → 3(defun fibset (f1 f2)
(lcons f1 (fibset f2 (+ f1 f2))))(setf q (fibset 1 1))(lcar (lcdr (lcdr (lcdr (lcdr q))))) →
5
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 47
![Page 344: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/344.jpg)
In�nite Computations
I What does this recursion compute?
(defun numbers (x)(lcons x (numbers (1+ x))))
(setf p (numbers 0))(lcar p) → 0(lcdr p) → (1 . "#<DELAYED-COMPUTATION>")(lcar (lcdr p)) → 1(lcar (lcdr (lcdr p))) → 2(lcar (lcdr (lcdr (lcdr p)))) → 3(defun fibset (f1 f2)
(lcons f1 (fibset f2 (+ f1 f2))))(setf q (fibset 1 1))(lcar (lcdr (lcdr (lcdr (lcdr q))))) → 5
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 47
![Page 345: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/345.jpg)
lfind-if Function I
I returns �rst element of lazy-list satisfying predicate pred
(defun lfind-if (pred llist)(cond ((funcall
pred (lcar llist)) (lcar llist))( t (lfind-if pred (lcdr llist)))))
I Find smallest Fibonacci number greater than 342
(lfind-if(function (lambda (x) (>= x 342)))(fibset 1 1)) → 377
(lfind-if(function (lambda (x) (>= x 342)))(primeset)) → 347
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 48
![Page 346: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/346.jpg)
lfind-if Function I
I returns �rst element of lazy-list satisfying predicate pred
(defun lfind-if (pred llist)(cond ((funcall
pred (lcar llist)) (lcar llist))( t (lfind-if pred (lcdr llist)))))
I Find smallest Fibonacci number greater than 342
(lfind-if(function (lambda (x) (>= x 342)))(fibset 1 1)) →
377(lfind-if
(function (lambda (x) (>= x 342)))(primeset)) → 347
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 48
![Page 347: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/347.jpg)
lfind-if Function I
I returns �rst element of lazy-list satisfying predicate pred
(defun lfind-if (pred llist)(cond ((funcall
pred (lcar llist)) (lcar llist))( t (lfind-if pred (lcdr llist)))))
I Find smallest Fibonacci number greater than 342
(lfind-if(function (lambda (x) (>= x 342)))(fibset 1 1)) → 377
(lfind-if(function (lambda (x) (>= x 342)))(primeset)) → 347
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 48
![Page 348: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/348.jpg)
lfind-if Function I
I returns �rst element of lazy-list satisfying predicate pred
(defun lfind-if (pred llist)(cond ((funcall
pred (lcar llist)) (lcar llist))( t (lfind-if pred (lcdr llist)))))
I Find smallest Fibonacci number greater than 342
(lfind-if(function (lambda (x) (>= x 342)))(fibset 1 1)) → 377
(lfind-if(function (lambda (x) (>= x 342)))(primeset)) →
347
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 48
![Page 349: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/349.jpg)
lfind-if Function I
I returns �rst element of lazy-list satisfying predicate pred
(defun lfind-if (pred llist)(cond ((funcall
pred (lcar llist)) (lcar llist))( t (lfind-if pred (lcdr llist)))))
I Find smallest Fibonacci number greater than 342
(lfind-if(function (lambda (x) (>= x 342)))(fibset 1 1)) → 377
(lfind-if(function (lambda (x) (>= x 342)))(primeset)) → 347
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 48
![Page 350: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/350.jpg)
lfind-if Function II
I As written, lfind-if may never return(lfind-if
(function (lambda (x) (< x 0)))(fibset 1 1)) →
ERROR: STACK OVERFLOWI Logic of set generator is decoupled from predicate tests
I Functional model without state or side-e�ects(setf p (numbers 0))(lcar (lcdr (lcdr p))) → 2
(lcar p) → 0
(setf q (lcons 9 (lcdr p)))I In�nite sequence is not altered by accessors
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 49
![Page 351: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/351.jpg)
lfind-if Function II
I As written, lfind-if may never return(lfind-if
(function (lambda (x) (< x 0)))(fibset 1 1)) → ERROR: STACK OVERFLOW
I Logic of set generator is decoupled from predicate tests
I Functional model without state or side-e�ects(setf p (numbers 0))(lcar (lcdr (lcdr p))) → 2
(lcar p) → 0
(setf q (lcons 9 (lcdr p)))I In�nite sequence is not altered by accessors
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 49
![Page 352: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/352.jpg)
lfind-if Function II
I As written, lfind-if may never return(lfind-if
(function (lambda (x) (< x 0)))(fibset 1 1)) → ERROR: STACK OVERFLOW
I Logic of set generator is decoupled from predicate tests
I Functional model without state or side-e�ects(setf p (numbers 0))(lcar (lcdr (lcdr p))) → 2
(lcar p) → 0
(setf q (lcons 9 (lcdr p)))I In�nite sequence is not altered by accessors
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 49
![Page 353: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/353.jpg)
lfind-if Function II
I As written, lfind-if may never return(lfind-if
(function (lambda (x) (< x 0)))(fibset 1 1)) → ERROR: STACK OVERFLOW
I Logic of set generator is decoupled from predicate tests
I Functional model without state or side-e�ects
(setf p (numbers 0))(lcar (lcdr (lcdr p))) → 2
(lcar p) → 0
(setf q (lcons 9 (lcdr p)))I In�nite sequence is not altered by accessors
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 49
![Page 354: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/354.jpg)
lfind-if Function II
I As written, lfind-if may never return(lfind-if
(function (lambda (x) (< x 0)))(fibset 1 1)) → ERROR: STACK OVERFLOW
I Logic of set generator is decoupled from predicate tests
I Functional model without state or side-e�ects(setf p (numbers 0))
(lcar (lcdr (lcdr p))) → 2
(lcar p) → 0
(setf q (lcons 9 (lcdr p)))I In�nite sequence is not altered by accessors
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 49
![Page 355: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/355.jpg)
lfind-if Function II
I As written, lfind-if may never return(lfind-if
(function (lambda (x) (< x 0)))(fibset 1 1)) → ERROR: STACK OVERFLOW
I Logic of set generator is decoupled from predicate tests
I Functional model without state or side-e�ects(setf p (numbers 0))(lcar (lcdr (lcdr p))) →
2
(lcar p) → 0
(setf q (lcons 9 (lcdr p)))I In�nite sequence is not altered by accessors
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 49
![Page 356: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/356.jpg)
lfind-if Function II
I As written, lfind-if may never return(lfind-if
(function (lambda (x) (< x 0)))(fibset 1 1)) → ERROR: STACK OVERFLOW
I Logic of set generator is decoupled from predicate tests
I Functional model without state or side-e�ects(setf p (numbers 0))(lcar (lcdr (lcdr p))) → 2
(lcar p) → 0
(setf q (lcons 9 (lcdr p)))I In�nite sequence is not altered by accessors
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 49
![Page 357: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/357.jpg)
lfind-if Function II
I As written, lfind-if may never return(lfind-if
(function (lambda (x) (< x 0)))(fibset 1 1)) → ERROR: STACK OVERFLOW
I Logic of set generator is decoupled from predicate tests
I Functional model without state or side-e�ects(setf p (numbers 0))(lcar (lcdr (lcdr p))) → 2
(lcar p) →
0
(setf q (lcons 9 (lcdr p)))I In�nite sequence is not altered by accessors
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 49
![Page 358: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/358.jpg)
lfind-if Function II
I As written, lfind-if may never return(lfind-if
(function (lambda (x) (< x 0)))(fibset 1 1)) → ERROR: STACK OVERFLOW
I Logic of set generator is decoupled from predicate tests
I Functional model without state or side-e�ects(setf p (numbers 0))(lcar (lcdr (lcdr p))) → 2
(lcar p) → 0
(setf q (lcons 9 (lcdr p)))I In�nite sequence is not altered by accessors
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 49
![Page 359: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/359.jpg)
lfind-if Function II
I As written, lfind-if may never return(lfind-if
(function (lambda (x) (< x 0)))(fibset 1 1)) → ERROR: STACK OVERFLOW
I Logic of set generator is decoupled from predicate tests
I Functional model without state or side-e�ects(setf p (numbers 0))(lcar (lcdr (lcdr p))) → 2
(lcar p) → 0
(setf q (lcons 9 (lcdr p)))
I In�nite sequence is not altered by accessors
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 49
![Page 360: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/360.jpg)
lfind-if Function II
I As written, lfind-if may never return(lfind-if
(function (lambda (x) (< x 0)))(fibset 1 1)) → ERROR: STACK OVERFLOW
I Logic of set generator is decoupled from predicate tests
I Functional model without state or side-e�ects(setf p (numbers 0))(lcar (lcdr (lcdr p))) → 2
(lcar p) → 0
(setf q (lcons 9 (lcdr p)))I In�nite sequence is not altered by accessors
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 49
![Page 361: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/361.jpg)
Simpli�ed Implementation of Laziness
I De�ne delay to freeze evaluation of expressions in originallexical context(defmacro delay (form)
`(function (lambda () ,form)))
I De�ne force to restart computation(defmacro force (delayed-expression)
`(funcall ,delayed-expression))I Lazy list operators
(defmacro lcons (car cdr) `(cons ,car (delay ,cdr)))(defmacro lcar (cell) `(car ,cell))(defmacro lcdr (cell) `(force (cdr ,cell)))
I A more complex version might de�ne a type for delayedcomputations
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 50
![Page 362: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/362.jpg)
Simpli�ed Implementation of Laziness
I De�ne delay to freeze evaluation of expressions in originallexical context(defmacro delay (form)
`(function (lambda () ,form)))I De�ne force to restart computation
(defmacro force (delayed-expression)`(funcall ,delayed-expression))
I Lazy list operators
(defmacro lcons (car cdr) `(cons ,car (delay ,cdr)))(defmacro lcar (cell) `(car ,cell))(defmacro lcdr (cell) `(force (cdr ,cell)))
I A more complex version might de�ne a type for delayedcomputations
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 50
![Page 363: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/363.jpg)
Simpli�ed Implementation of Laziness
I De�ne delay to freeze evaluation of expressions in originallexical context(defmacro delay (form)
`(function (lambda () ,form)))I De�ne force to restart computation
(defmacro force (delayed-expression)`(funcall ,delayed-expression))
I Lazy list operators
(defmacro lcons (car cdr) `(cons ,car (delay ,cdr)))(defmacro lcar (cell) `(car ,cell))(defmacro lcdr (cell) `(force (cdr ,cell)))
I A more complex version might de�ne a type for delayedcomputations
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 50
![Page 364: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/364.jpg)
Simpli�ed Implementation of Laziness
I De�ne delay to freeze evaluation of expressions in originallexical context(defmacro delay (form)
`(function (lambda () ,form)))I De�ne force to restart computation
(defmacro force (delayed-expression)`(funcall ,delayed-expression))
I Lazy list operators
(defmacro lcons (car cdr) `(cons ,car (delay ,cdr)))
(defmacro lcar (cell) `(car ,cell))(defmacro lcdr (cell) `(force (cdr ,cell)))
I A more complex version might de�ne a type for delayedcomputations
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 50
![Page 365: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/365.jpg)
Simpli�ed Implementation of Laziness
I De�ne delay to freeze evaluation of expressions in originallexical context(defmacro delay (form)
`(function (lambda () ,form)))I De�ne force to restart computation
(defmacro force (delayed-expression)`(funcall ,delayed-expression))
I Lazy list operators
(defmacro lcons (car cdr) `(cons ,car (delay ,cdr)))(defmacro lcar (cell) `(car ,cell))
(defmacro lcdr (cell) `(force (cdr ,cell)))I A more complex version might de�ne a type for delayed
computations
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 50
![Page 366: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/366.jpg)
Simpli�ed Implementation of Laziness
I De�ne delay to freeze evaluation of expressions in originallexical context(defmacro delay (form)
`(function (lambda () ,form)))I De�ne force to restart computation
(defmacro force (delayed-expression)`(funcall ,delayed-expression))
I Lazy list operators
(defmacro lcons (car cdr) `(cons ,car (delay ,cdr)))(defmacro lcar (cell) `(car ,cell))(defmacro lcdr (cell) `(force (cdr ,cell)))
I A more complex version might de�ne a type for delayedcomputations
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 50
![Page 367: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004](https://reader033.vdocuments.site/reader033/viewer/2022052612/5f0b42fd7e708231d42fa4bd/html5/thumbnails/367.jpg)
Simpli�ed Implementation of Laziness
I De�ne delay to freeze evaluation of expressions in originallexical context(defmacro delay (form)
`(function (lambda () ,form)))I De�ne force to restart computation
(defmacro force (delayed-expression)`(funcall ,delayed-expression))
I Lazy list operators
(defmacro lcons (car cdr) `(cons ,car (delay ,cdr)))(defmacro lcar (cell) `(car ,cell))(defmacro lcdr (cell) `(force (cdr ,cell)))
I A more complex version might de�ne a type for delayedcomputations
B. Price and R. Greiner CMPUT325: Meta-programming Fundamentals 50