Class 5: Procedure Practice

16
Class 5: Procedure Practice cs1120 Fall 2011 David Evans 2 September 2011

description

If ExpressionsWhy we can’t define if ourselvesPractice defining procedures

Transcript of Class 5: Procedure Practice

Page 1: Class 5: Procedure Practice

Class 5:Procedure Practice

cs1120 Fall 2011David Evans2 September 2011

Page 2: Class 5: Procedure Practice

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.)

Page 3: Class 5: Procedure Practice

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.

Page 4: Class 5: Procedure Practice

If Examples

(if false false true)

(if (> 4 3) 4 3)

((lambda (a b) (if (> a b) a b)) 5 6)

Page 5: Class 5: Procedure Practice

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)

Page 6: Class 5: Procedure Practice

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!

Page 7: Class 5: Procedure Practice

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?)

Page 8: Class 5: Procedure Practice

8

Language Elements

Question from Class 2:When learning a foreign language, which

elements are hardest to learn?

Page 9: Class 5: Procedure Practice

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?

Page 10: Class 5: Procedure Practice

Pages in Revised5 Report on the Algorithmic Language Scheme

Primitives

Means of Combination

Means of Abstraction

48 pages total (includes formal specification and examples)

Page 11: Class 5: Procedure Practice

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)

Page 12: Class 5: Procedure Practice

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)

Page 13: Class 5: Procedure Practice

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!

Page 14: Class 5: Procedure Practice

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)

Page 15: Class 5: Procedure Practice

How do you learn a language?

How do you learn a new way of thinking?

Page 16: Class 5: Procedure Practice

Rest of Today

Procedures Practice