Download presentation
Presentation is loading. Please wait.
1
six 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 to be able to say ► [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 inputs It returns an output 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 +, -, ×, / (division), etc. You type the × symbol by typing the * key 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
A more interesting procedure We haven’t taught you enough at this point to understand how iterated-group works You’ll understand in a few weeks For the moment, just copy it from this slide and use it But the thing we do want you to understand is that You give it a procedure that makes pictures And a number of times to call it And it gives you a group of all the results of calling the procedure with the arguments 0, 1, 2, etc. ► [define iterated-group [proc count → [apply group [up-to count proc]]]]
11
Using iterated-group [iterated-group [n → [line [point [× n 20] 0] [point [× n 20] 300]]] 10]
12
Using iterated-group [iterated-group [n → [ink [pen 'black n] [line [point [× n 20] 0] [point [× n 20] 300]]]] 21]
13
Using iterated-group ► [iterated-group [n → [translate [point [× n 20] [× n 20]] [box 10 10]]] 5] ►
14
Using iterated-group ► [iterated-group [n → [translate [point 0 [× n 10]] [box 10 10]]] 5] ►
15
Using iterated-group ► [iterated-group [n → [translate [point [× n 10] [× [sin [ ⁄ n 2]] 30]] [box 10 10]]] 20] ►
16
What about this one? ► [iterated-group [m → [iterated-group [n → [translate [point [× m 10] [× n 10]] [box 10 10]]] 5] 5] ? ►
17
What about this one? ► [iterated-group [m → [iterated-group [n → [translate [point [× m 10] [× n 10]] [box 10 10]]] 5] 5] ►
18
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 ►
19
Boolean objects Everything in Meta is an expression, And (almost) all expressions have values, So then 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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.