CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B....

367

Transcript of CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B....

Page 1: CMPUT325: Meta-programming Fundamentalsrgreiner/C-325/...CMPUT325: Meta-programming Fundamentals B. Price and R. Greiner 29th September 2004

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

�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

�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

�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

�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

�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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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