Class 4: Making Procedures
-
Upload
david-evans -
Category
Technology
-
view
539 -
download
0
description
Transcript of Class 4: Making Procedures
![Page 1: Class 4: Making Procedures](https://reader033.fdocuments.us/reader033/viewer/2022061202/547c6a965906b59a798b474a/html5/thumbnails/1.jpg)
Class 4: Making Procedures
cs1120 Fall 2011David Evans30 August 2011
![Page 2: Class 4: Making Procedures](https://reader033.fdocuments.us/reader033/viewer/2022061202/547c6a965906b59a798b474a/html5/thumbnails/2.jpg)
Announcements
PS1 is due Monday (electronic submission and paper submission): don’t wait to get started!
Quiz 1 is Wednesday (in class)Chapters 1-4 of Course BookChapters 1-3 of The InformationClasses 1-5 (including questions from class notes)
I haven’t forgotten about answering your questions from PS0. I will post my answers by tomorrow.
![Page 3: Class 4: Making Procedures](https://reader033.fdocuments.us/reader033/viewer/2022061202/547c6a965906b59a798b474a/html5/thumbnails/3.jpg)
3
Recap: Assigning MeaningsProgram ::= ε | ProgramElement ProgramProgramElement ::= Expression | DefinitionDefinition ::= (define Name Expression)Expression ::= PrimitiveExpression | NameExpression
| ApplicationExpression | ProcedureExpression | IfExpressionPrimitiveExpression ::= Number | true | false| PrimitiveProcedureNameExpression ::= Name ApplicationExpression ::= (Expression MoreExpressions)MoreExpressions ::= ε | Expression MoreExpressionsProcedureExpression ::= (lambda (Parameters) Expression)Parameters ::= ε | Name ParametersIfExpression ::= (if ExpressionPred ExpressionConsequent ExpressionAlt)
This grammar generates (nearly) all surface forms in the Scheme. language. If we have a meaning rule for each grammar rule, we can determine the meaning of every Scheme program.
![Page 4: Class 4: Making Procedures](https://reader033.fdocuments.us/reader033/viewer/2022061202/547c6a965906b59a798b474a/html5/thumbnails/4.jpg)
4
Evaluation Rules: Last ClassPrimitiveExpression ::= Number | true | false| PrimitiveProcedure
Rule 1: If the expression is a primitive, it evaluates to its pre-defined value.
NameExpression ::= Name
Rule 2: A name evaluates to the value associated with that name.ApplicationExpression ::= (Expression MoreExpressions)MoreExpressions ::= ε | Expression MoreExpressions
Rule 3: To evaluate an application expression:a) Evaluate all the subexpressions (in any order)b) Apply the value of the first subexpression to the values of all the other
subexpressions.
![Page 5: Class 4: Making Procedures](https://reader033.fdocuments.us/reader033/viewer/2022061202/547c6a965906b59a798b474a/html5/thumbnails/5.jpg)
5
Last class: Rules for Application1. Primitives. If the procedure to apply is a
primitive procedure, just do it.
2. Constructed Procedures. If the procedure is a constructed procedure, evaluate the body of the procedure with each parameter name bound to the corresponding input expression value.
This only makes sense if we know what a constructed procedure is!
![Page 6: Class 4: Making Procedures](https://reader033.fdocuments.us/reader033/viewer/2022061202/547c6a965906b59a798b474a/html5/thumbnails/6.jpg)
6
Constructing ProceduresProgram ::= ε | ProgramElement ProgramProgramElement ::= Expression | DefinitionDefinition ::= (define Name Expression)Expression ::= PrimitiveExpression | NameExpression
| ApplicationExpression | ProcedureExpression | IfExpressionPrimitiveExpression ::= Number | true | false| PrimitiveProcedureNameExpression ::= Name ApplicationExpression ::= (Expression MoreExpressions)MoreExpressions ::= ε | Expression MoreExpressions
ProcedureExpression ::= (lambda (Parameters) Expression)Parameters ::= ε | Name ParametersIfExpression ::= (if ExpressionPred ExpressionConsequent ExpressionAlt)
![Page 7: Class 4: Making Procedures](https://reader033.fdocuments.us/reader033/viewer/2022061202/547c6a965906b59a798b474a/html5/thumbnails/7.jpg)
7
Constructing Procedures
lambda means “make a procedure”
Expression ::= ProcedureExpressionProcedureExpression ::= (lambda (Parameters) Expression) Parameters ::= εParameters ::= Name Parameters
![Page 8: Class 4: Making Procedures](https://reader033.fdocuments.us/reader033/viewer/2022061202/547c6a965906b59a798b474a/html5/thumbnails/8.jpg)
8
Evaluation Rule 4: Lambda
A lambda expression evaluates to a procedure that takes the given parameters and has the expression as its body.
ProcedureExpression ::= (lambda (Parameters) Expression)Parameters ::= ε | Name Parameters
![Page 9: Class 4: Making Procedures](https://reader033.fdocuments.us/reader033/viewer/2022061202/547c6a965906b59a798b474a/html5/thumbnails/9.jpg)
Lambda Expressions
(lambda () true)
(lambda (x) (* x x))
(lambda (a) (lambda (b) (+ a b)))
![Page 10: Class 4: Making Procedures](https://reader033.fdocuments.us/reader033/viewer/2022061202/547c6a965906b59a798b474a/html5/thumbnails/10.jpg)
Applying Compound Procedures
((lambda () true) 1120)
Rule 3: To evaluate an application expression: (a) Evaluate all the subexpressions (in any order) (b) Apply the value of the first subexpression to the values of all the other subexpressions.
Apply Rule for constructed procedures: Evaluate the body of the procedure with each parameter name bound to the corresponding input expression value.
Evaluation Rule 4. A lambda expression evaluates to a procedure that takes the given parameters and has the expression as its body.
![Page 11: Class 4: Making Procedures](https://reader033.fdocuments.us/reader033/viewer/2022061202/547c6a965906b59a798b474a/html5/thumbnails/11.jpg)
Applying Compound Procedures
((lambda (x) (+ x 1000)) 120)
Rule 3: To evaluate an application expression: (a) Evaluate all the subexpressions (in any order) (b) Apply the value of the first subexpression to the values of all the other subexpressions.
Apply Rule for constructed procedures: Evaluate the body of the procedure with each parameter name bound to the corresponding input expression value.
Evaluation Rule 4. A lambda expression evaluates to a procedure that takes the given parameters and has the expression as its body.
![Page 12: Class 4: Making Procedures](https://reader033.fdocuments.us/reader033/viewer/2022061202/547c6a965906b59a798b474a/html5/thumbnails/12.jpg)
Applying Compound Procedures
((lambda (x) (+ x 1000)) x)
Rule 3: To evaluate an application expression: (a) Evaluate all the subexpressions (in any order) (b) Apply the value of the first subexpression to the values of all the other subexpressions.
Apply Rule for constructed procedures: Evaluate the body of the procedure with each parameter name bound to the corresponding input expression value.
Evaluation Rule 4. A lambda expression evaluates to a procedure that takes the given parameters and has the expression as its body.
![Page 13: Class 4: Making Procedures](https://reader033.fdocuments.us/reader033/viewer/2022061202/547c6a965906b59a798b474a/html5/thumbnails/13.jpg)
Applying Compound Procedures
((lambda (a) (lambda (b) (+ a b))) 5)
Rule 3: To evaluate an application expression: (a) Evaluate all the subexpressions (in any order) (b) Apply the value of the first subexpression to the values of all the other subexpressions.
Apply Rule for constructed procedures: Evaluate the body of the procedure with each parameter name bound to the corresponding input expression value.
Evaluation Rule 4. A lambda expression evaluates to a procedure that takes the given parameters and has the expression as its body.
![Page 14: Class 4: Making Procedures](https://reader033.fdocuments.us/reader033/viewer/2022061202/547c6a965906b59a798b474a/html5/thumbnails/14.jpg)
Applying Compound Procedures
(((lambda (a) (lambda (b) (+ a b))) 5) 6)
Rule 3: To evaluate an application expression: (a) Evaluate all the subexpressions (in any order) (b) Apply the value of the first subexpression to the values of all the other subexpressions.
Apply Rule for constructed procedures: Evaluate the body of the procedure with each parameter name bound to the corresponding input expression value.
Evaluation Rule 4. A lambda expression evaluates to a procedure that takes the given parameters and has the expression as its body.
![Page 15: Class 4: Making Procedures](https://reader033.fdocuments.us/reader033/viewer/2022061202/547c6a965906b59a798b474a/html5/thumbnails/15.jpg)
Do we have everything we need to describe all
computations?
![Page 16: Class 4: Making Procedures](https://reader033.fdocuments.us/reader033/viewer/2022061202/547c6a965906b59a798b474a/html5/thumbnails/16.jpg)
16
Language Elements
Question from Class 2:When learning a foreign language, which
elements are hardest to learn?
PrimitivesMeans of CombinationMeans of Abstraction
![Page 17: Class 4: Making Procedures](https://reader033.fdocuments.us/reader033/viewer/2022061202/547c6a965906b59a798b474a/html5/thumbnails/17.jpg)
17
Primitives: lots of them, and hard to learn real meaning (but its just memorization)
Means of CombinationComplex, but, all natural languages have similar ones [Chomsky]
Sentence ::= Subject Object Verb (45%)
Sentence ::= Subject Verb Object (42%)
Sentence ::= Verb Subject Object (9%)
Sentence ::= Object Subject Verb (<1%)
Scheme:
Means of Abstraction: few of these, but tricky to learn differences across languagesTok Pisin (Papua New Guinea): mi (I), mitupela (he/she and I), mitripela
(both of them and I), mipela (all of them and I), yumitupela (you and I), yumitripela (both of you and I), yumipela (all of you and I)
Scheme:
Welsh: “Lladdodd y ddraig y dyn.”
![Page 18: Class 4: Making Procedures](https://reader033.fdocuments.us/reader033/viewer/2022061202/547c6a965906b59a798b474a/html5/thumbnails/18.jpg)
Charge
PS1: Due MondayWednesdays, 5-6:30pm (Jiamin, Rice 1xx)Thursdays, 9:45-11am (Dave, Rice 507)Thursdays, 10-11:30am (Peter, Rice 1xx)Thursdays, 1-2:30pm (Joseph, Rice 1xx)Thursdays, 4:30-6pm (Jonathan, Rice 1xx)Thursdays, 6-7:30pm (Jiamin, Rice 1xx)
By FridayFinish reading Chapters 1-4, Gleick Chapters 1-3
Quiz 1: Wednesday