class 5: procedure practice
DESCRIPTION
If ExpressionsWhy we can’t define if ourselvesPractice defining proceduresTRANSCRIPT
Class 5:Procedure Practice
cs1120 Fall 2011David Evans2 September 2011
2
The Language so Far…Definition ::= (define Name Expression)Expression ::= PrimitiveExpression | NameExpression
| ApplicationExpression | ProcedureExpressionPrimitiveExpression ::= Number | true | false| PrimitiveProcedureNameExpression ::= Name ApplicationExpression ::= (Expression MoreExpressions)MoreExpressions ::= ε | Expression MoreExpressionsProcedureExpression ::= (lambda (Parameters) Expression)Parameters ::= ε | Name Parameters
Enough for closer-color? (PS1). Is it enough for all programs?
Almost….if the evaluation rules were a little different (~PS7) this would really be enough! With these evaluation rules, we need one more thing. (But we won’t understand why until about Class 15.)
3
Evaluation Rule 5: IfIfExpression
::= (if ExpressionPredicate ExpressionConsequent ExpressionAlternate)
To evaluate an if expression:(a) Evaluate ExpressionPredicate.
(b) If it evaluates to a false value, the value of the if expression is the value of ExpressionAlternate; otherwise, the value of the if expression is the value of ExpressionConsequent.
If Examples
(if false false true)
(if (> 4 3) 4 3)
((lambda (a b) (if (> a b) a b)) 5 6)
Programs
A program is evaluated by evaluating each expression or definition in order.
Program ::= ε | ProgramElement ProgramProgramElement ::= Expression | Definition
(define x 3)(define x 7)x
x(define x 3)(define x 7)
6
Completeness of Evaluation RulesProgram ::= ε | 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)
Since we have an evaluation rule for each grammar rule, we can determine the meaning of any Scheme program!
7
Now You Can Write Any Program!
You know enough now to define a procedure that performs every possible computation!We’ll prove this later in the course
We’ll learn some more useful Scheme forms:There are a few more special forms (like if)But, none of these are necessary…just helpful
We have not defined the evaluation rules precisely enough to unambiguously understand all programs (e.g., what does “value associated with a name” mean?)
8
Language Elements
Question from Class 2:When learning a foreign language, which
elements are hardest to learn?
9
Primitives: lots of them, and hard to learn real meaning (but its just memorization)
Means of Combination: complex, but similar in all languagesEnglish: Sentence ::= Subject Verb Object (42%)Scheme: ApplicationExpression ::= (ExpressionVerb MoreExpressionsObject)
Java: ApplicationExpression ::= Object Verb Object (e.g., 2 + 3) ApplicationExpression ::= Object.Verb(Object)
(e.g., System.out.println(“Hello!”)) Means of Abstraction: few, but tricky
Tok Pisin (Papua New Guinea): mi (I), mitupela (he/she and I), mitripela (both of them and I), yumitupela (you and I), …
Scheme:
When learning a foreign language, which elements are hardest to learn?
Pages in Revised5 Report on the Algorithmic Language Scheme
Primitives
Means of Combination
Means of Abstraction
48 pages total (includes formal specification and examples)
Pages in Revised5 Report on the Algorithmic Language Scheme
Primitives
Standard ProceduresPrimitive expressionsIdentifiers, numerals
18 21
Means of Combination
ExpressionsProgram structure
22
Means of Abstraction
Definitions ½
48 pages total (includes formal specification and examples)
Pages in Revised5 Report on the Algorithmic Language Scheme
Pages in C++ Language Specification (1998)
Primitives
Standard ProceduresPrimitive expressionsIdentifiers, numerals
18 21
Means of Combination
ExpressionsProgram structure
22
Means of Abstraction
Definitions ½
48 pages total (includes formal specification and examples)
Pages in Revised5 Report on the Algorithmic Language Scheme
Pages in C++ Language Specification (1998)
Primitives
Standard ProceduresPrimitive expressionsIdentifiers, numerals
18 21
Standard ProceduresPrimitive expressionsIdentifiers, numerals
356 3010
Means of Combination
ExpressionsProgram structure
22
Expressions, StatementsProgram Structure
19735
Means of Abstraction
Definitions ½ Declarations, Classes 173
48 pages total (includes formal specification and examples)
776 pages total (includes no formal specification or examples)
C++ Core language issues list has 948 items!
Pages in Revised5 Report on the Algorithmic Language Scheme English
PrimitivesStandard ProceduresPrimitive expressionsIdentifiers, numerals
18 21
MorphemesWords in Oxford English Dictionary
?500,000
Means of Combination
ExpressionsProgram structure
22
Grammar RulesEnglish Grammar for Dummies Book
100s (?)
384 pages
Means of Abstraction
Definitions ½ Pronouns ~20
48 pages total (includes formal specification and examples)
How do you learn a language?
How do you learn a new way of thinking?
Rest of Today
Procedures Practice