Upcoming SlideShare
×

# Class 4: Making Procedures

507 views

Published on

Lambda
Making and Applying Procedures
SVO/SOV/OVS Languages

Published in: Technology, Education
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
507
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
4
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Class 4: Making Procedures

1. 1. Class 4:MakingProcedurescs1120 Fall 2011David Evans30 August 2011
2. 2. AnnouncementsPS1 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 Book Chapters 1-3 of The Information Classes 1-5 (including questions from class notes) I haven’t forgotten about answering your questions from PS0. I will post my answers by tomorrow.
3. 3. Recap: Assigning MeaningsProgram ::= ε | ProgramElement ProgramProgramElement ::= Expression | DefinitionDefinition ::= (define Name Expression)Expression ::= PrimitiveExpression | NameExpression | ApplicationExpression | ProcedureExpression | IfExpressionPrimitiveExpression ::= Number | true | false| PrimitiveProcedureNameExpression ::= NameApplicationExpression ::= (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 candetermine the meaning of every Scheme program. 3
4. 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. 4
5. 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! 5
6. 6. Constructing ProceduresProgram ::= ε | ProgramElement ProgramProgramElement ::= Expression | DefinitionDefinition ::= (define Name Expression)Expression ::= PrimitiveExpression | NameExpression | ApplicationExpression | ProcedureExpression | IfExpressionPrimitiveExpression ::= Number | true | false| PrimitiveProcedureNameExpression ::= NameApplicationExpression ::= (Expression MoreExpressions)MoreExpressions ::= ε | Expression MoreExpressionsProcedureExpression ::= (lambda (Parameters) Expression)Parameters ::= ε | Name ParametersIfExpression ::= (if ExpressionPred ExpressionConsequent ExpressionAlt) 6
7. 7. Constructing Procedures lambda means “make a procedure”Expression ::= ProcedureExpressionProcedureExpression ::= (lambda (Parameters) Expression)Parameters ::= εParameters ::= Name Parameters 7
8. 8. Evaluation Rule 4: LambdaA lambda expression evaluates to aprocedure that takes the givenparameters and has the expression asits body.ProcedureExpression ::= (lambda (Parameters) Expression)Parameters ::= ε | Name Parameters 8
9. 9. Lambda Expressions(lambda () true)(lambda (x) (* x x))(lambda (a) (lambda (b) (+ a b)))
10. 10. Applying Compound Procedures Rule 3: To evaluate an application expression:((lambda () (a) Evaluate all the subexpressions (in any order) true) (b) Apply the value of the first subexpression to the values of all the other subexpressions. 1120) 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.
11. 11. Applying Compound Procedures Rule 3: To evaluate an application expression:((lambda (x) (a) Evaluate all the subexpressions (in any order) (+ x 1000)) (b) Apply the value of the first subexpression to the values of all the other subexpressions. 120) 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.
12. 12. Applying Compound Procedures Rule 3: To evaluate an application expression:((lambda (x) (a) Evaluate all the subexpressions (in any order) (+ x 1000)) (b) Apply the value of the first subexpression to the values of all the other subexpressions. x) 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.
13. 13. Applying Compound Procedures Rule 3: To evaluate an application expression:((lambda (a) (a) Evaluate all the subexpressions (in any order) (lambda (b) (b) Apply the value of the first subexpression to the values of all the other subexpressions. (+ a b))) Apply Rule for constructed procedures: 5) 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.
14. 14. Applying Compound Procedures Rule 3: To evaluate an application expression:(((lambda (a) (a) Evaluate all the subexpressions (in any order) (lambda (b) (b) Apply the value of the first subexpression to the values of all the other subexpressions. (+ a b))) Apply Rule for constructed procedures: 5) Evaluate the body of the procedure with each parameter name bound to the corresponding 6) 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.
15. 15. Do we have everything we need to describe all computations?
16. 16. Language Elements Question from Class 2:When learning a foreign language, which elements are hardest to learn?PrimitivesMeans of CombinationMeans of Abstraction 16
17. 17. Primitives: lots of them, and hard to learn real meaning (but its just memorization)Means of Combination Complex, but, all natural languages have similar ones [Chomsky] Sentence ::= Subject Object Verb (45%) Sentence ::= Subject Verb Object (42%) Welsh: “Lladdodd y ddraig y dyn.” Sentence ::= Verb Subject Object (9%) Sentence ::= Object Subject Verb (<1%) Scheme:Means of Abstraction: few of these, but tricky to learn differences across languages Tok 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: 17
18. 18. ChargePS1: Due Monday Wednesdays, 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 Friday Finish reading Chapters 1-4, Gleick Chapters 1-3Quiz 1: Wednesday