61a extra lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-macros_1pp.pdf · (demo)...
TRANSCRIPT
![Page 1: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/1.jpg)
61A Extra Lecture 8
![Page 2: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/2.jpg)
Announcements
![Page 3: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/3.jpg)
Homoiconicity
![Page 4: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/4.jpg)
A Scheme Expression is a Scheme List
4
![Page 5: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/5.jpg)
A Scheme Expression is a Scheme List
Scheme programs consist of expressions, which can be:
4
![Page 6: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/6.jpg)
A Scheme Expression is a Scheme List
Scheme programs consist of expressions, which can be:
•Primitive expressions: 2 3.3 true + quotient
4
![Page 7: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/7.jpg)
A Scheme Expression is a Scheme List
Scheme programs consist of expressions, which can be:
•Primitive expressions: 2 3.3 true + quotient•Combinations: (quotient 10 2) (not true)
4
![Page 8: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/8.jpg)
A Scheme Expression is a Scheme List
Scheme programs consist of expressions, which can be:
•Primitive expressions: 2 3.3 true + quotient•Combinations: (quotient 10 2) (not true)
4
The built-in Scheme list data structure (which is a linked list) can represent combinations
![Page 9: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/9.jpg)
A Scheme Expression is a Scheme List
Scheme programs consist of expressions, which can be:
•Primitive expressions: 2 3.3 true + quotient•Combinations: (quotient 10 2) (not true)
4
scm> (list 'quotient 10 2)
The built-in Scheme list data structure (which is a linked list) can represent combinations
![Page 10: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/10.jpg)
A Scheme Expression is a Scheme List
Scheme programs consist of expressions, which can be:
•Primitive expressions: 2 3.3 true + quotient•Combinations: (quotient 10 2) (not true)
4
scm> (list 'quotient 10 2)(quotient 10 2)
The built-in Scheme list data structure (which is a linked list) can represent combinations
![Page 11: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/11.jpg)
A Scheme Expression is a Scheme List
Scheme programs consist of expressions, which can be:
•Primitive expressions: 2 3.3 true + quotient•Combinations: (quotient 10 2) (not true)
4
scm> (list 'quotient 10 2)(quotient 10 2)
The built-in Scheme list data structure (which is a linked list) can represent combinations
![Page 12: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/12.jpg)
A Scheme Expression is a Scheme List
Scheme programs consist of expressions, which can be:
•Primitive expressions: 2 3.3 true + quotient•Combinations: (quotient 10 2) (not true)
4
scm> (list 'quotient 10 2)(quotient 10 2)
scm> (eval (list 'quotient 10 2))
The built-in Scheme list data structure (which is a linked list) can represent combinations
![Page 13: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/13.jpg)
A Scheme Expression is a Scheme List
Scheme programs consist of expressions, which can be:
•Primitive expressions: 2 3.3 true + quotient•Combinations: (quotient 10 2) (not true)
4
scm> (list 'quotient 10 2)(quotient 10 2)
scm> (eval (list 'quotient 10 2))5
The built-in Scheme list data structure (which is a linked list) can represent combinations
![Page 14: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/14.jpg)
A Scheme Expression is a Scheme List
Scheme programs consist of expressions, which can be:
•Primitive expressions: 2 3.3 true + quotient•Combinations: (quotient 10 2) (not true)
4
scm> (list 'quotient 10 2)(quotient 10 2)
scm> (eval (list 'quotient 10 2))5
The built-in Scheme list data structure (which is a linked list) can represent combinations
![Page 15: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/15.jpg)
A Scheme Expression is a Scheme List
Scheme programs consist of expressions, which can be:
•Primitive expressions: 2 3.3 true + quotient•Combinations: (quotient 10 2) (not true)
4
scm> (list 'quotient 10 2)(quotient 10 2)
scm> (eval (list 'quotient 10 2))5
The built-in Scheme list data structure (which is a linked list) can represent combinations
In such a language, it is straightforward to write a program that writes a program
![Page 16: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/16.jpg)
A Scheme Expression is a Scheme List
Scheme programs consist of expressions, which can be:
•Primitive expressions: 2 3.3 true + quotient•Combinations: (quotient 10 2) (not true)
4
scm> (list 'quotient 10 2)(quotient 10 2)
scm> (eval (list 'quotient 10 2))5
(Demo)
The built-in Scheme list data structure (which is a linked list) can represent combinations
In such a language, it is straightforward to write a program that writes a program
![Page 17: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/17.jpg)
Homoiconic Languages
5
![Page 18: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/18.jpg)
Homoiconic Languages
Languages have both a concrete syntax and an abstract syntax
5
![Page 19: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/19.jpg)
Homoiconic Languages
Languages have both a concrete syntax and an abstract syntax
(Python Demo)
5
![Page 20: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/20.jpg)
Homoiconic Languages
Languages have both a concrete syntax and an abstract syntax
(Python Demo)
A language is homoiconic if the abstract syntax can be read from the concrete syntax
5
![Page 21: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/21.jpg)
Homoiconic Languages
Languages have both a concrete syntax and an abstract syntax
(Python Demo)
A language is homoiconic if the abstract syntax can be read from the concrete syntax
(Scheme Demo)
5
![Page 22: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/22.jpg)
Homoiconic Languages
Languages have both a concrete syntax and an abstract syntax
(Python Demo)
A language is homoiconic if the abstract syntax can be read from the concrete syntax
(Scheme Demo)
Quotation is actually a combination in disguise
5
![Page 23: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/23.jpg)
Homoiconic Languages
Languages have both a concrete syntax and an abstract syntax
(Python Demo)
A language is homoiconic if the abstract syntax can be read from the concrete syntax
(Scheme Demo)
Quotation is actually a combination in disguise
(Quote Demo)
5
![Page 24: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/24.jpg)
Homoiconic Languages
Languages have both a concrete syntax and an abstract syntax
(Python Demo)
A language is homoiconic if the abstract syntax can be read from the concrete syntax
(Scheme Demo)
Quotation is actually a combination in disguise
(Quote Demo)
5
![Page 25: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/25.jpg)
Macros
![Page 26: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/26.jpg)
Macros Perform Code Transformations
7
![Page 27: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/27.jpg)
Macros Perform Code Transformations
A macro is an operation performed on the source code of a program before evaluation
7
![Page 28: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/28.jpg)
Macros Perform Code Transformations
A macro is an operation performed on the source code of a program before evaluation
Macros exist in many languages, but are easiest to define correctly in homoiconic languages
7
![Page 29: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/29.jpg)
Macros Perform Code Transformations
A macro is an operation performed on the source code of a program before evaluation
Macros exist in many languages, but are easiest to define correctly in homoiconic languages
Scheme has a define-macro special form that defines a source code transformation
7
![Page 30: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/30.jpg)
Macros Perform Code Transformations
A macro is an operation performed on the source code of a program before evaluation
Macros exist in many languages, but are easiest to define correctly in homoiconic languages
Scheme has a define-macro special form that defines a source code transformation
7
(define-macro (twice expr) (list 'begin expr expr))
![Page 31: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/31.jpg)
Macros Perform Code Transformations
A macro is an operation performed on the source code of a program before evaluation
Macros exist in many languages, but are easiest to define correctly in homoiconic languages
Scheme has a define-macro special form that defines a source code transformation
7
(define-macro (twice expr) (list 'begin expr expr))
> (twice (print 2)) 2 2
![Page 32: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/32.jpg)
Macros Perform Code Transformations
A macro is an operation performed on the source code of a program before evaluation
Macros exist in many languages, but are easiest to define correctly in homoiconic languages
Scheme has a define-macro special form that defines a source code transformation
7
(define-macro (twice expr) (list 'begin expr expr))
> (twice (print 2)) 2 2
Evaluation procedure of a macro call expression:
![Page 33: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/33.jpg)
Macros Perform Code Transformations
A macro is an operation performed on the source code of a program before evaluation
Macros exist in many languages, but are easiest to define correctly in homoiconic languages
Scheme has a define-macro special form that defines a source code transformation
7
(define-macro (twice expr) (list 'begin expr expr))
> (twice (print 2)) 2 2
Evaluation procedure of a macro call expression:
• Evaluate the operator sub-expression, which evaluates to a macro
![Page 34: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/34.jpg)
Macros Perform Code Transformations
A macro is an operation performed on the source code of a program before evaluation
Macros exist in many languages, but are easiest to define correctly in homoiconic languages
Scheme has a define-macro special form that defines a source code transformation
7
(define-macro (twice expr) (list 'begin expr expr))
> (twice (print 2)) 2 2
Evaluation procedure of a macro call expression:
• Evaluate the operator sub-expression, which evaluates to a macro
• Call the macro procedure on the operand expressions without evaluating them first
![Page 35: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/35.jpg)
Macros Perform Code Transformations
A macro is an operation performed on the source code of a program before evaluation
Macros exist in many languages, but are easiest to define correctly in homoiconic languages
Scheme has a define-macro special form that defines a source code transformation
7
(define-macro (twice expr) (list 'begin expr expr))
> (twice (print 2)) 2 2
Evaluation procedure of a macro call expression:
• Evaluate the operator sub-expression, which evaluates to a macro
• Call the macro procedure on the operand expressions without evaluating them first
• Evaluate the expression returned from the macro procedure
![Page 36: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/36.jpg)
Macros Perform Code Transformations
A macro is an operation performed on the source code of a program before evaluation
Macros exist in many languages, but are easiest to define correctly in homoiconic languages
Scheme has a define-macro special form that defines a source code transformation
7
(define-macro (twice expr) (list 'begin expr expr))
> (twice (print 2)) 2 2
Evaluation procedure of a macro call expression:
• Evaluate the operator sub-expression, which evaluates to a macro
• Call the macro procedure on the operand expressions without evaluating them first
• Evaluate the expression returned from the macro procedure
(Demo)
![Page 37: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/37.jpg)
Problem 1
Define a macro that evaluates an expression for each value in a sequence
8
![Page 38: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/38.jpg)
Problem 1
Define a macro that evaluates an expression for each value in a sequence
8
(define (map fn vals)
![Page 39: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/39.jpg)
Problem 1
Define a macro that evaluates an expression for each value in a sequence
8
(define (map fn vals) (if (null? vals)
![Page 40: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/40.jpg)
Problem 1
Define a macro that evaluates an expression for each value in a sequence
8
(define (map fn vals) (if (null? vals) ()
![Page 41: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/41.jpg)
Problem 1
Define a macro that evaluates an expression for each value in a sequence
8
(define (map fn vals) (if (null? vals) () (cons (fn (car vals))
![Page 42: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/42.jpg)
Problem 1
Define a macro that evaluates an expression for each value in a sequence
8
(define (map fn vals) (if (null? vals) () (cons (fn (car vals)) (map fn (cdr vals)))))
![Page 43: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/43.jpg)
Problem 1
Define a macro that evaluates an expression for each value in a sequence
8
(define (map fn vals) (if (null? vals) () (cons (fn (car vals)) (map fn (cdr vals)))))
![Page 44: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/44.jpg)
Problem 1
Define a macro that evaluates an expression for each value in a sequence
8
(define (map fn vals) (if (null? vals) () (cons (fn (car vals)) (map fn (cdr vals)))))
scm> (map (lambda (x) (* x x)) '(2 3 4 5))
![Page 45: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/45.jpg)
Problem 1
Define a macro that evaluates an expression for each value in a sequence
8
(define (map fn vals) (if (null? vals) () (cons (fn (car vals)) (map fn (cdr vals)))))
scm> (map (lambda (x) (* x x)) '(2 3 4 5))(4 9 16 25)
![Page 46: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/46.jpg)
Problem 1
Define a macro that evaluates an expression for each value in a sequence
8
(define (map fn vals) (if (null? vals) () (cons (fn (car vals)) (map fn (cdr vals)))))
scm> (map (lambda (x) (* x x)) '(2 3 4 5))(4 9 16 25)
scm> (for x (* x x) '(2 3 4 5)) (4 9 16 25)
![Page 47: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/47.jpg)
Problem 1
Define a macro that evaluates an expression for each value in a sequence
8
(define (map fn vals) (if (null? vals) () (cons (fn (car vals)) (map fn (cdr vals)))))
scm> (map (lambda (x) (* x x)) '(2 3 4 5))(4 9 16 25)
scm> (for x (* x x) '(2 3 4 5)) (4 9 16 25)
(define-macro (for sym expr vals)
(list 'map _____________________________________________________________________________)
![Page 48: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/48.jpg)
Problem 1
Define a macro that evaluates an expression for each value in a sequence
8
(define (map fn vals) (if (null? vals) () (cons (fn (car vals)) (map fn (cdr vals)))))
scm> (map (lambda (x) (* x x)) '(2 3 4 5))(4 9 16 25)
scm> (for x (* x x) '(2 3 4 5)) (4 9 16 25)
(define-macro (for sym expr vals)
(list 'map _____________________________________________________________________________)(list 'lambda (list sym) expr) vals)
![Page 49: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/49.jpg)
Quasi-Quoting
(Demo)
![Page 50: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/50.jpg)
Variable-Length Parameter Lists
(Demo)
![Page 51: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/51.jpg)
Problem 2
Define a function nest that builds a nested list containing its arguments
11
![Page 52: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/52.jpg)
Problem 2
Define a function nest that builds a nested list containing its arguments
11
(define (nest first . rest)
(if (null? rest)
________________________________________________________________
________________________________________________________________))
![Page 53: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/53.jpg)
Problem 2
Define a function nest that builds a nested list containing its arguments
11
(define (nest first . rest)
(if (null? rest)
________________________________________________________________
________________________________________________________________))
scm> (nest 3) (3)
scm> (nest 3 4 5 6) (3 (4 (5 (6))))
![Page 54: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/54.jpg)
Problem 2
Define a function nest that builds a nested list containing its arguments
11
(define (nest first . rest)
(if (null? rest)
________________________________________________________________
________________________________________________________________))
scm> (nest 3) (3)
scm> (nest 3 4 5 6) (3 (4 (5 (6))))
(list first)
![Page 55: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/55.jpg)
Problem 2
Define a function nest that builds a nested list containing its arguments
11
(define (nest first . rest)
(if (null? rest)
________________________________________________________________
________________________________________________________________))
scm> (nest 3) (3)
scm> (nest 3 4 5 6) (3 (4 (5 (6))))
(list first)
(list first (apply nest rest))
![Page 56: 61A Extra Lecture 8inst.eecs.berkeley.edu/~cs61a/fa15/assets/slides/e08-Macros_1pp.pdf · (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations](https://reader033.vdocuments.mx/reader033/viewer/2022060608/605f7e4cf2da295860327534/html5/thumbnails/56.jpg)
Temporary Symbols
(Demo)