61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 ·...
Transcript of 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 ·...
![Page 1: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/1.jpg)
61A Lecture 27
Wednesday, November 5
![Page 2: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/2.jpg)
Announcements
2
![Page 3: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/3.jpg)
Announcements
• Homework 7 due Wednesday 11/5 @ 11:59pm
2
![Page 4: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/4.jpg)
Announcements
• Homework 7 due Wednesday 11/5 @ 11:59pm
• Project 1 composition revisions due Wednesday 11/5 @ 11:59pm
2
![Page 5: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/5.jpg)
Announcements
• Homework 7 due Wednesday 11/5 @ 11:59pm
• Project 1 composition revisions due Wednesday 11/5 @ 11:59pm
!Copy hog.py to an instructional server and run: submit proj1revision
2
![Page 6: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/6.jpg)
Announcements
• Homework 7 due Wednesday 11/5 @ 11:59pm
• Project 1 composition revisions due Wednesday 11/5 @ 11:59pm
!Copy hog.py to an instructional server and run: submit proj1revision
• Quiz 2 released Wednesday 11/5 & due Thursday 11/6 @ 11:59pm
2
![Page 7: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/7.jpg)
Announcements
• Homework 7 due Wednesday 11/5 @ 11:59pm
• Project 1 composition revisions due Wednesday 11/5 @ 11:59pm
!Copy hog.py to an instructional server and run: submit proj1revision
• Quiz 2 released Wednesday 11/5 & due Thursday 11/6 @ 11:59pm
!Open note, open interpreter, closed classmates, closed Internet
2
![Page 8: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/8.jpg)
Announcements
• Homework 7 due Wednesday 11/5 @ 11:59pm
• Project 1 composition revisions due Wednesday 11/5 @ 11:59pm
!Copy hog.py to an instructional server and run: submit proj1revision
• Quiz 2 released Wednesday 11/5 & due Thursday 11/6 @ 11:59pm
!Open note, open interpreter, closed classmates, closed Internet
• Midterm survey due Monday 11/10 @ 11:59pm (Thanks!)
2
![Page 9: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/9.jpg)
Announcements
• Homework 7 due Wednesday 11/5 @ 11:59pm
• Project 1 composition revisions due Wednesday 11/5 @ 11:59pm
!Copy hog.py to an instructional server and run: submit proj1revision
• Quiz 2 released Wednesday 11/5 & due Thursday 11/6 @ 11:59pm
!Open note, open interpreter, closed classmates, closed Internet
• Midterm survey due Monday 11/10 @ 11:59pm (Thanks!)
• Project 4 due Thursday 11/20 @ 11:59pm (Big!)
2
![Page 10: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/10.jpg)
Interpreting Scheme
![Page 11: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/11.jpg)
The Structure of an Interpreter
4
![Page 12: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/12.jpg)
The Structure of an Interpreter
4
Apply
Eval
![Page 13: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/13.jpg)
The Structure of an Interpreter
4
Apply
EvalBase cases:
![Page 14: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/14.jpg)
The Structure of an Interpreter
4
Apply
EvalBase cases: • Primitive values (numbers)
![Page 15: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/15.jpg)
The Structure of an Interpreter
4
Apply
Eval
Recursive calls:
Base cases: • Primitive values (numbers)
![Page 16: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/16.jpg)
The Structure of an Interpreter
4
Apply
Eval
Recursive calls: • Eval(operator, operands) of call expressions
Base cases: • Primitive values (numbers)
![Page 17: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/17.jpg)
The Structure of an Interpreter
4
Apply
Eval
Recursive calls: • Eval(operator, operands) of call expressions• Apply(procedure, arguments)
Base cases: • Primitive values (numbers)
![Page 18: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/18.jpg)
The Structure of an Interpreter
4
Apply
Eval
Recursive calls: • Eval(operator, operands) of call expressions• Apply(procedure, arguments)
Base cases: • Primitive values (numbers)
Base cases: • Built-in primitive procedures
![Page 19: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/19.jpg)
The Structure of an Interpreter
4
Apply
Eval
Recursive calls: • Eval(operator, operands) of call expressions• Apply(procedure, arguments)
Base cases: • Primitive values (numbers)• Look up values bound to symbols
Base cases: • Built-in primitive procedures
![Page 20: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/20.jpg)
The Structure of an Interpreter
4
Apply
Eval
Recursive calls: • Eval(operator, operands) of call expressions• Apply(procedure, arguments)
Base cases: • Primitive values (numbers)• Look up values bound to symbols
Base cases: • Built-in primitive procedures
![Page 21: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/21.jpg)
The Structure of an Interpreter
4
Apply
Eval
Recursive calls: • Eval(operator, operands) of call expressions• Apply(procedure, arguments)• Eval(sub-expressions) of special forms
Base cases: • Primitive values (numbers)• Look up values bound to symbols
Base cases: • Built-in primitive procedures
![Page 22: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/22.jpg)
The Structure of an Interpreter
4
Apply
Eval
Recursive calls: • Eval(operator, operands) of call expressions• Apply(procedure, arguments)• Eval(sub-expressions) of special forms
Base cases: • Primitive values (numbers)• Look up values bound to symbols
Base cases: • Built-in primitive procedures Recursive calls: • Eval(body) of user-defined procedures
![Page 23: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/23.jpg)
The Structure of an Interpreter
4
Apply
Eval
Recursive calls: • Eval(operator, operands) of call expressions• Apply(procedure, arguments)• Eval(sub-expressions) of special forms
Base cases: • Primitive values (numbers)• Look up values bound to symbols
Base cases: • Built-in primitive procedures Recursive calls: • Eval(body) of user-defined procedures
![Page 24: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/24.jpg)
The Structure of an Interpreter
4
Apply
Eval
Recursive calls: • Eval(operator, operands) of call expressions• Apply(procedure, arguments)• Eval(sub-expressions) of special forms
Base cases: • Primitive values (numbers)• Look up values bound to symbols
Base cases: • Built-in primitive procedures Recursive calls: • Eval(body) of user-defined procedures
Requires an environment for symbol
lookup
![Page 25: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/25.jpg)
The Structure of an Interpreter
4
Apply
Eval
Recursive calls: • Eval(operator, operands) of call expressions• Apply(procedure, arguments)• Eval(sub-expressions) of special forms
Base cases: • Primitive values (numbers)• Look up values bound to symbols
Base cases: • Built-in primitive procedures Recursive calls: • Eval(body) of user-defined procedures
Requires an environment for symbol
lookup
Creates a new environment each time
a user-defined procedure is applied
![Page 26: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/26.jpg)
Special Forms
![Page 27: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/27.jpg)
Scheme Evaluation
6
![Page 28: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/28.jpg)
Scheme Evaluation
The scheme_eval function dispatches on expression form:
6
![Page 29: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/29.jpg)
Scheme Evaluation
The scheme_eval function dispatches on expression form:
• Symbols are looked up in the current environment
6
![Page 30: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/30.jpg)
Scheme Evaluation
The scheme_eval function dispatches on expression form:
• Symbols are looked up in the current environment
• Self-evaluating expressions are returned as values
6
![Page 31: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/31.jpg)
Scheme Evaluation
The scheme_eval function dispatches on expression form:
• Symbols are looked up in the current environment
• Self-evaluating expressions are returned as values
• All other legal expressions are represented as Scheme lists, called combinations
6
![Page 32: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/32.jpg)
Scheme Evaluation
The scheme_eval function dispatches on expression form:
• Symbols are looked up in the current environment
• Self-evaluating expressions are returned as values
• All other legal expressions are represented as Scheme lists, called combinations
(if <predicate> <consequent> <alternative>)
6
![Page 33: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/33.jpg)
Scheme Evaluation
The scheme_eval function dispatches on expression form:
• Symbols are looked up in the current environment
• Self-evaluating expressions are returned as values
• All other legal expressions are represented as Scheme lists, called combinations
(if <predicate> <consequent> <alternative>)
(lambda (<formal-parameters>) <body>)
6
![Page 34: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/34.jpg)
Scheme Evaluation
The scheme_eval function dispatches on expression form:
• Symbols are looked up in the current environment
• Self-evaluating expressions are returned as values
• All other legal expressions are represented as Scheme lists, called combinations
(if <predicate> <consequent> <alternative>)
(define <name> <expression>)
(lambda (<formal-parameters>) <body>)
6
![Page 35: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/35.jpg)
Scheme Evaluation
The scheme_eval function dispatches on expression form:
• Symbols are looked up in the current environment
• Self-evaluating expressions are returned as values
• All other legal expressions are represented as Scheme lists, called combinations
(if <predicate> <consequent> <alternative>)
(define <name> <expression>)
(lambda (<formal-parameters>) <body>)
(<operator> <operand 0> ... <operand k>)
6
![Page 36: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/36.jpg)
Scheme Evaluation
The scheme_eval function dispatches on expression form:
• Symbols are looked up in the current environment
• Self-evaluating expressions are returned as values
• All other legal expressions are represented as Scheme lists, called combinations
(if <predicate> <consequent> <alternative>)
(define <name> <expression>)
(lambda (<formal-parameters>) <body>)
(<operator> <operand 0> ... <operand k>)
Special forms are identified by the first list element
6
![Page 37: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/37.jpg)
Scheme Evaluation
The scheme_eval function dispatches on expression form:
• Symbols are looked up in the current environment
• Self-evaluating expressions are returned as values
• All other legal expressions are represented as Scheme lists, called combinations
(if <predicate> <consequent> <alternative>)
(define <name> <expression>)
(lambda (<formal-parameters>) <body>)
(<operator> <operand 0> ... <operand k>)
Special forms are identified by the first list element
6
![Page 38: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/38.jpg)
Scheme Evaluation
The scheme_eval function dispatches on expression form:
• Symbols are looked up in the current environment
• Self-evaluating expressions are returned as values
• All other legal expressions are represented as Scheme lists, called combinations
(if <predicate> <consequent> <alternative>)
(define <name> <expression>)
(lambda (<formal-parameters>) <body>)
(<operator> <operand 0> ... <operand k>)
Special forms are identified by the first list element
Any combination that is not a known special form is a call
expression
6
![Page 39: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/39.jpg)
Scheme Evaluation
The scheme_eval function dispatches on expression form:
• Symbols are looked up in the current environment
• Self-evaluating expressions are returned as values
• All other legal expressions are represented as Scheme lists, called combinations
(if <predicate> <consequent> <alternative>)
(define <name> <expression>)
(lambda (<formal-parameters>) <body>)
(<operator> <operand 0> ... <operand k>)
Special forms are identified by the first list element
Any combination that is not a known special form is a call
expression
(define (demo s) (if (null? s) '(3) (cons (car s) (demo (cdr s))) ))
6
![Page 40: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/40.jpg)
Scheme Evaluation
The scheme_eval function dispatches on expression form:
• Symbols are looked up in the current environment
• Self-evaluating expressions are returned as values
• All other legal expressions are represented as Scheme lists, called combinations
(if <predicate> <consequent> <alternative>)
(define <name> <expression>)
(lambda (<formal-parameters>) <body>)
(<operator> <operand 0> ... <operand k>)
Special forms are identified by the first list element
Any combination that is not a known special form is a call
expression
(define (demo s) (if (null? s) '(3) (cons (car s) (demo (cdr s))) ))
(demo (list 1 2))
6
![Page 41: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/41.jpg)
Logical Forms
![Page 42: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/42.jpg)
Logical Special Forms
8
![Page 43: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/43.jpg)
Logical Special Forms
Logical forms may only evaluate some sub-expressions.
8
![Page 44: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/44.jpg)
Logical Special Forms
Logical forms may only evaluate some sub-expressions.
• If expression: (if <predicate> <consequent> <alternative>)
8
![Page 45: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/45.jpg)
Logical Special Forms
Logical forms may only evaluate some sub-expressions.
• If expression: (if <predicate> <consequent> <alternative>)
• And and or: (and <e1> ... <en>), (or <e1> ... <en>)
8
![Page 46: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/46.jpg)
Logical Special Forms
Logical forms may only evaluate some sub-expressions.
• If expression: (if <predicate> <consequent> <alternative>)
• And and or: (and <e1> ... <en>), (or <e1> ... <en>)
• Cond expression: (cond (<p1> <e1>) ... (<pn> <en>) (else <e>))
8
![Page 47: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/47.jpg)
Logical Special Forms
Logical forms may only evaluate some sub-expressions.
• If expression: (if <predicate> <consequent> <alternative>)
• And and or: (and <e1> ... <en>), (or <e1> ... <en>)
• Cond expression: (cond (<p1> <e1>) ... (<pn> <en>) (else <e>))
The value of an if expression is the value of a sub-expression:
8
![Page 48: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/48.jpg)
Logical Special Forms
Logical forms may only evaluate some sub-expressions.
• If expression: (if <predicate> <consequent> <alternative>)
• And and or: (and <e1> ... <en>), (or <e1> ... <en>)
• Cond expression: (cond (<p1> <e1>) ... (<pn> <en>) (else <e>))
The value of an if expression is the value of a sub-expression:
• Evaluate the predicate.
8
![Page 49: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/49.jpg)
Logical Special Forms
Logical forms may only evaluate some sub-expressions.
• If expression: (if <predicate> <consequent> <alternative>)
• And and or: (and <e1> ... <en>), (or <e1> ... <en>)
• Cond expression: (cond (<p1> <e1>) ... (<pn> <en>) (else <e>))
The value of an if expression is the value of a sub-expression:
• Evaluate the predicate.
• Choose a sub-expression: <consequent> or <alternative>.
8
![Page 50: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/50.jpg)
Logical Special Forms
Logical forms may only evaluate some sub-expressions.
• If expression: (if <predicate> <consequent> <alternative>)
• And and or: (and <e1> ... <en>), (or <e1> ... <en>)
• Cond expression: (cond (<p1> <e1>) ... (<pn> <en>) (else <e>))
The value of an if expression is the value of a sub-expression:
• Evaluate the predicate.
• Choose a sub-expression: <consequent> or <alternative>.
• Evaluate that sub-expression in place of the whole expression.
8
![Page 51: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/51.jpg)
Logical Special Forms
Logical forms may only evaluate some sub-expressions.
• If expression: (if <predicate> <consequent> <alternative>)
• And and or: (and <e1> ... <en>), (or <e1> ... <en>)
• Cond expression: (cond (<p1> <e1>) ... (<pn> <en>) (else <e>))
The value of an if expression is the value of a sub-expression:
• Evaluate the predicate.
• Choose a sub-expression: <consequent> or <alternative>.
• Evaluate that sub-expression in place of the whole expression.
do_if_form
8
![Page 52: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/52.jpg)
Logical Special Forms
Logical forms may only evaluate some sub-expressions.
• If expression: (if <predicate> <consequent> <alternative>)
• And and or: (and <e1> ... <en>), (or <e1> ... <en>)
• Cond expression: (cond (<p1> <e1>) ... (<pn> <en>) (else <e>))
The value of an if expression is the value of a sub-expression:
• Evaluate the predicate.
• Choose a sub-expression: <consequent> or <alternative>.
• Evaluate that sub-expression in place of the whole expression.
do_if_form
scheme_eval
8
![Page 53: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/53.jpg)
Logical Special Forms
Logical forms may only evaluate some sub-expressions.
• If expression: (if <predicate> <consequent> <alternative>)
• And and or: (and <e1> ... <en>), (or <e1> ... <en>)
• Cond expression: (cond (<p1> <e1>) ... (<pn> <en>) (else <e>))
The value of an if expression is the value of a sub-expression:
• Evaluate the predicate.
• Choose a sub-expression: <consequent> or <alternative>.
• Evaluate that sub-expression in place of the whole expression.
do_if_form
scheme_eval
(Demo)
8
![Page 54: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/54.jpg)
Quotation
![Page 55: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/55.jpg)
Quotation
10
![Page 56: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/56.jpg)
Quotation
The quote special form evaluates to the quoted expression, which is not evaluated
10
![Page 57: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/57.jpg)
Quotation
The quote special form evaluates to the quoted expression, which is not evaluated
(quote <expression>)
10
![Page 58: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/58.jpg)
Quotation
The quote special form evaluates to the quoted expression, which is not evaluated
(quote <expression>)
10
(quote (+ 1 2)) (+ 1 2)evaluates to the three-element Scheme list
![Page 59: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/59.jpg)
Quotation
The quote special form evaluates to the quoted expression, which is not evaluated
(quote <expression>)
The <expression> itself is the value of the whole quote expression
10
(quote (+ 1 2)) (+ 1 2)evaluates to the three-element Scheme list
![Page 60: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/60.jpg)
Quotation
The quote special form evaluates to the quoted expression, which is not evaluated
(quote <expression>)
The <expression> itself is the value of the whole quote expression
'<expression> is shorthand for (quote <expression>)
10
(quote (+ 1 2)) (+ 1 2)evaluates to the three-element Scheme list
![Page 61: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/61.jpg)
Quotation
The quote special form evaluates to the quoted expression, which is not evaluated
(quote <expression>)
The <expression> itself is the value of the whole quote expression
'<expression> is shorthand for (quote <expression>)
10
(quote (+ 1 2)) (+ 1 2)evaluates to the three-element Scheme list
(quote (1 2)) '(1 2)is equivalent to
![Page 62: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/62.jpg)
Quotation
The quote special form evaluates to the quoted expression, which is not evaluated
(quote <expression>)
The <expression> itself is the value of the whole quote expression
'<expression> is shorthand for (quote <expression>)
The scheme_read parser converts shorthand ' to a combination that starts with quote
10
(quote (+ 1 2)) (+ 1 2)evaluates to the three-element Scheme list
(quote (1 2)) '(1 2)is equivalent to
![Page 63: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/63.jpg)
Quotation
The quote special form evaluates to the quoted expression, which is not evaluated
(quote <expression>)
The <expression> itself is the value of the whole quote expression
'<expression> is shorthand for (quote <expression>)
The scheme_read parser converts shorthand ' to a combination that starts with quote
10
(Demo)
(quote (+ 1 2)) (+ 1 2)evaluates to the three-element Scheme list
(quote (1 2)) '(1 2)is equivalent to
![Page 64: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/64.jpg)
Lambda Expressions
![Page 65: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/65.jpg)
Lambda Expressions
12
![Page 66: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/66.jpg)
Lambda Expressions
Lambda expressions evaluate to user-defined procedures.
12
![Page 67: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/67.jpg)
Lambda Expressions
Lambda expressions evaluate to user-defined procedures.
(lambda (<formal-parameters>) <body>)
12
![Page 68: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/68.jpg)
Lambda Expressions
Lambda expressions evaluate to user-defined procedures.
(lambda (<formal-parameters>) <body>)
(lambda (x) (* x x))
12
![Page 69: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/69.jpg)
Lambda Expressions
Lambda expressions evaluate to user-defined procedures.
(lambda (<formal-parameters>) <body>)
(lambda (x) (* x x))
class LambdaProcedure:
def __init__(self, formals, body, env):
self.formals = formals
self.body = body
self.env = env
12
![Page 70: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/70.jpg)
Lambda Expressions
Lambda expressions evaluate to user-defined procedures.
(lambda (<formal-parameters>) <body>)
(lambda (x) (* x x))
class LambdaProcedure:
def __init__(self, formals, body, env):
self.formals = formals
self.body = body
self.env = env
12
A scheme list of symbols
![Page 71: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/71.jpg)
Lambda Expressions
Lambda expressions evaluate to user-defined procedures.
(lambda (<formal-parameters>) <body>)
(lambda (x) (* x x))
class LambdaProcedure:
def __init__(self, formals, body, env):
self.formals = formals
self.body = body
self.env = env
12
A scheme list of symbolsA scheme expression
![Page 72: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/72.jpg)
Lambda Expressions
Lambda expressions evaluate to user-defined procedures.
(lambda (<formal-parameters>) <body>)
(lambda (x) (* x x))
class LambdaProcedure:
def __init__(self, formals, body, env):
self.formals = formals
self.body = body
self.env = env
12
A scheme list of symbolsA scheme expressionA Frame instance
![Page 73: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/73.jpg)
Frames and Environments
13
![Page 74: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/74.jpg)
Frames and Environments
A frame represents an environment by having a parent frame
13
![Page 75: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/75.jpg)
Frames and Environments
A frame represents an environment by having a parent frame
Frames are Python instances with methods lookup and define
13
![Page 76: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/76.jpg)
Frames and Environments
A frame represents an environment by having a parent frame
Frames are Python instances with methods lookup and define
In Project 4, Frames do not hold return values
13
![Page 77: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/77.jpg)
Frames and Environments
A frame represents an environment by having a parent frame
Frames are Python instances with methods lookup and define
In Project 4, Frames do not hold return values
g: Global frame
yz
35
13
![Page 78: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/78.jpg)
Frames and Environments
A frame represents an environment by having a parent frame
Frames are Python instances with methods lookup and define
In Project 4, Frames do not hold return values
g: Global frame
yz
35
f1: [parent=g]
xz
24
13
![Page 79: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/79.jpg)
Frames and Environments
A frame represents an environment by having a parent frame
Frames are Python instances with methods lookup and define
In Project 4, Frames do not hold return values
g: Global frame
yz
35
f1: [parent=g]
xz
24
13
(Demo)
![Page 80: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/80.jpg)
Define Expressions
![Page 81: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/81.jpg)
Define Expressions
15
![Page 82: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/82.jpg)
Define Expressions
Define binds a symbol to a value in the first frame of the current environment.
15
![Page 83: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/83.jpg)
Define Expressions
Define binds a symbol to a value in the first frame of the current environment.
(define <name> <expression>)
15
![Page 84: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/84.jpg)
Define Expressions
Define binds a symbol to a value in the first frame of the current environment.
(define <name> <expression>)
1. Evaluate the <expression>
15
![Page 85: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/85.jpg)
Define Expressions
Define binds a symbol to a value in the first frame of the current environment.
(define <name> <expression>)
1. Evaluate the <expression>
2. Bind <name> to its value in the current frame
15
![Page 86: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/86.jpg)
Define Expressions
Define binds a symbol to a value in the first frame of the current environment.
(define <name> <expression>)
1. Evaluate the <expression>
2. Bind <name> to its value in the current frame
(define x (+ 1 2))
15
![Page 87: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/87.jpg)
Define Expressions
Define binds a symbol to a value in the first frame of the current environment.
(define <name> <expression>)
Procedure definition is shorthand of define with a lambda expression
1. Evaluate the <expression>
2. Bind <name> to its value in the current frame
(define x (+ 1 2))
15
![Page 88: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/88.jpg)
Define Expressions
Define binds a symbol to a value in the first frame of the current environment.
(define <name> <expression>)
(define (<name> <formal parameters>) <body>)
Procedure definition is shorthand of define with a lambda expression
1. Evaluate the <expression>
2. Bind <name> to its value in the current frame
(define x (+ 1 2))
15
![Page 89: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/89.jpg)
Define Expressions
Define binds a symbol to a value in the first frame of the current environment.
(define <name> <expression>)
(define (<name> <formal parameters>) <body>)
(define <name> (lambda (<formal parameters>) <body>))
Procedure definition is shorthand of define with a lambda expression
1. Evaluate the <expression>
2. Bind <name> to its value in the current frame
(define x (+ 1 2))
15
![Page 90: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/90.jpg)
Applying User-Defined Procedures
16
![Page 91: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/91.jpg)
Applying User-Defined Procedures
To apply a user-defined procedure, create a new frame in which formal parameters are bound to argument values, whose parent is the env attribute of the procedure
16
![Page 92: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/92.jpg)
Applying User-Defined Procedures
To apply a user-defined procedure, create a new frame in which formal parameters are bound to argument values, whose parent is the env attribute of the procedure
Evaluate the body of the procedure in the environment that starts with this new frame
16
![Page 93: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/93.jpg)
Applying User-Defined Procedures
To apply a user-defined procedure, create a new frame in which formal parameters are bound to argument values, whose parent is the env attribute of the procedure
Evaluate the body of the procedure in the environment that starts with this new frame
(define (demo s) (if (null? s) '(3) (cons (car s) (demo (cdr s)))))
16
![Page 94: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/94.jpg)
Applying User-Defined Procedures
To apply a user-defined procedure, create a new frame in which formal parameters are bound to argument values, whose parent is the env attribute of the procedure
Evaluate the body of the procedure in the environment that starts with this new frame
(define (demo s) (if (null? s) '(3) (cons (car s) (demo (cdr s)))))
g: Global frame
demo LambdaProcedure instance [parent=g]
16
![Page 95: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/95.jpg)
Applying User-Defined Procedures
To apply a user-defined procedure, create a new frame in which formal parameters are bound to argument values, whose parent is the env attribute of the procedure
Evaluate the body of the procedure in the environment that starts with this new frame
(define (demo s) (if (null? s) '(3) (cons (car s) (demo (cdr s)))))
(demo (list 1 2))
g: Global frame
demo LambdaProcedure instance [parent=g]
16
![Page 96: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/96.jpg)
Applying User-Defined Procedures
To apply a user-defined procedure, create a new frame in which formal parameters are bound to argument values, whose parent is the env attribute of the procedure
Evaluate the body of the procedure in the environment that starts with this new frame
(define (demo s) (if (null? s) '(3) (cons (car s) (demo (cdr s)))))
(demo (list 1 2))
1
Pair
2
Pair
nil
g: Global frame
demo LambdaProcedure instance [parent=g]
16
![Page 97: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/97.jpg)
Applying User-Defined Procedures
To apply a user-defined procedure, create a new frame in which formal parameters are bound to argument values, whose parent is the env attribute of the procedure
Evaluate the body of the procedure in the environment that starts with this new frame
(define (demo s) (if (null? s) '(3) (cons (car s) (demo (cdr s)))))
(demo (list 1 2))
1
Pair
2
Pair
nil[parent=g] s
g: Global frame
demo LambdaProcedure instance [parent=g]
16
![Page 98: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/98.jpg)
Applying User-Defined Procedures
To apply a user-defined procedure, create a new frame in which formal parameters are bound to argument values, whose parent is the env attribute of the procedure
Evaluate the body of the procedure in the environment that starts with this new frame
(define (demo s) (if (null? s) '(3) (cons (car s) (demo (cdr s)))))
(demo (list 1 2))
1
Pair
2
Pair
nil[parent=g] s
[parent=g] s
g: Global frame
demo LambdaProcedure instance [parent=g]
16
![Page 99: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/99.jpg)
Applying User-Defined Procedures
To apply a user-defined procedure, create a new frame in which formal parameters are bound to argument values, whose parent is the env attribute of the procedure
Evaluate the body of the procedure in the environment that starts with this new frame
(define (demo s) (if (null? s) '(3) (cons (car s) (demo (cdr s)))))
(demo (list 1 2))
1
Pair
2
Pair
nil[parent=g] s
[parent=g] s
[parent=g] s
g: Global frame
demo LambdaProcedure instance [parent=g]
16
![Page 100: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/100.jpg)
Eval/Apply in Lisp 1.5
17
![Page 101: 61A Lecture 27 - University of California, Berkeleycs61a/fa14/assets/slides/... · 2014-11-03 · Open note, open interpreter, closed classmates, closed Internet 2. Announcements](https://reader033.fdocuments.in/reader033/viewer/2022060213/5f05462a7e708231d4122657/html5/thumbnails/101.jpg)
Eval/Apply in Lisp 1.5
17