Presentation is loading. Please wait.

Presentation is loading. Please wait.

Eight compound procedures and higher-order procedures.

Similar presentations


Presentation on theme: "Eight compound procedures and higher-order procedures."— Presentation transcript:

1 eight compound procedures and higher-order procedures

2 A box figure ► [group [box 400 400] [translate [point 100 100] [box 70 70]] [translate [point 100 −100] [box 70 70]] [translate [point −100 −100] [box 70 70]] [translate [point −100 100] [box 70 70]]]

3 Simplifying with names ► [define frame [box 400 400]] ► [define little [box 70 70]] ► [group frame [translate [point 100 100] little] [translate [point 100 −100] little] [translate [point −100 −100] little] [translate [point −100 100] little]]

4 What’s wrong with this? Defining little and frame saves us a little bit of work, but … What we really want to do is to name the whole pattern of making shifted boxes… We need abstraction

5 What we want ► [define frame [box 400 400]] ► [group frame [shifted 100 100] [shifted 100 −100] [shifted −100 −100] [shifted −100 100]]

6 What kind of a thing is shifted? Well, it takes arguments It returns a value It must be a procedure… In fact, we even know what it should do It should make a point from its two arguments Make a box Translate the box by the point And return it

7 Compound procedures [arg 1 arg 2 … arg n → exp] Procedures are just another data object You can construct new procedures from old using the → operator When called, the procedure Sets the local names arg 1 arg 2 … arg n to the arguments passed to the procedure Computes the value of exp using the values of the arguments Returns the value of exp Note: you type the → symbol by first typing – and then >

8 Defining shifted ► [define shifted [x y → [translate [point x y] little]]] ► [group frame [shifted 100 100] [shifted 100 −100] [shifted −100 −100] [shifted −100 100]]

9 A boring example Meta includes procedures for the standard arithmetic operations like +, -, ×, etc. Square is a compound procedure built from × Polynomial is a compound procedure built from ×, +, and square Notice they both use the name n for their argument But it’s okay because they’re local names ► [define square [n → [× n n]]] ► [square 2] 4 ► [square 58.7] 3445.69 ► [define polynomial [n → [+ [square n] [× n 2] 4]]] ► [polynomial 32] 1092 ►

10 Conditionals: the if operator [if test consequent alternative] If test is true, Then evaluate and return consequent Otherwise, evaluate and return alternative Some useful tests [eq? a b] Checks if a and b are the same object [= a b], [> a b], [≤ a b], etc. Compares numbers [and test 1 test 2 … test n ] [or test 1 test 2 … test n ] [not test] Combines tests into more complicated tests ► [define abs [n → [if [> n 0] n [- n]]]] ► [abs 5] 5 ► [abs -5] 5 ►

11 Boolean objects But if everything is an expression, And all expressions have values, What kind of value is [= a b] ? Answer: true or false The system includes two magic data objects, true and false, which are used to represent the answers to tests


Download ppt "Eight compound procedures and higher-order procedures."

Similar presentations


Ads by Google