Download presentation
Presentation is loading. Please wait.
1
seven constructing simple procedures using abstraction
2
Review: rules for execution Look at the expression If it’s a number or string It’s its own value If it’s a name (i.e. a word) Look its value up in the dictionary (Check if it’s one of the special cases from the next slide) Otherwise it’s a procedure call [proc-expression arg-expression 1 … arg-expression n ] Execute all the subexpressions (proc-expression and arg-expression 1 through arg-expression n ) Run the value of, passing it the values of proc-expression, passing it the values of arg-expression 1 through arg-expression n as inputs Use its output as the value of the expression These rules are worth memorizing
3
Special cases If it starts with define [define name value-expression] Run value-expression Assign its value to name in the dictionary If it starts with the words with or with* [with name 1 = value-expression 1 … name last = value-expression last result-expression] Run the value-expressions Assign their values to their respective names in the dictionary Run result-expression Set the dictionary back the way it was Return the value from result-expression If it has a → inside it [name 1 … name last → result-expression] Make a procedure That names its inputs name 1 … name last And returns the value of result-expression (presumably using those names)
4
What is the value of this expression? [+ [× 2 2] 3]
5
What is the value of this expression? [+ [× 2 2] 3] 7
6
What is the value of this expression? [ [n → [+ n 1]] 3]
7
What is the value of this expression? [ [n → [+ n 1]] 3] Find the values of the subexpressions [n → [+ n 1]] is a procedure 3 is the number 3
8
What is the value of this expression? [ [n → [+ n 1]] 3] Find the values of the subexpressions [n → [+ n 1]] is a procdure 3 is the number 3 Okay, call the procedure with 3 as its argument Add n to the dictionary with the value 3 Execute [+ n 1] Find the values of the subexpressions + is a procedure n is 3 1 is 1 Call the procedure with 3 and 1 as arguments Output is 4 Output of procedure is 4 Value of the overall expression is 4
9
What is the value of this expression? [ [n → [+ n 1]] 3] 4 The rules are simple, but very subtle
10
Drawing a line How do we make a 300 pixel vertical line?
11
Drawing a line How do we make a 300 pixel vertical line? [line [point 0 0] [point 0 300]]
12
Drawing a line How do we make it two pixels wide? [line [point 0 0] [point 0 300]
13
Drawing a line How do we make it two pixels wide? [ink [pen “black” 2] [line [point 0 0] [point 0 300]]
14
Drawing a line How do we make it three pixels wide? [ink [pen “black” 2] [line [point 0 0] [point 0 300]]
15
Drawing a line How do we make it three pixels wide? [ink [pen “black” 3] [line [point 0 0] [point 0 300]]
16
Drawing a line How do we make a procedure that creates lines of specified widths? [ink [pen “black” 3] [line [point 0 0] [point 0 300]]
17
Drawing a line How do we make a procedure that creates lines of specified widths? [n → [ink [pen “black” n] [line [point 0 0] [point 0 300]]]
18
Drawing a line How do we make it shift the line right by n pixels? [n → [ink [pen “black” n] [line [point 0 0] [point 0 300]]]
19
Drawing a line How do we make it shift the line right by n pixels? [n → [ink [pen “black” n] [line [point n 0] [point n 300]]]
20
Drawing a line How do we make it shift the line more? [n → [ink [pen “black” n] [line [point n 0] [point n 300]]]
21
Drawing a line How do we make it shift the line more? [n → [ink [pen “black” n] [line [point [× 20 n] 0] [point [× 20 n] 300]]]
22
Theme and variation How do we get a whole series of lines of progressive widths?
23
Theme and variation [iterated-group [n → [ink [pen “black” n] [line [point [× n 20] 0] [point [× n 20] 300]]]] 21] generates a single line number of lines to make calls the line generator repeatedly and collects the results
24
Abstraction Adding variables to expressions make them less specific The expression: [line [point n 0] [point n 300]] doesn’t express any particular line It expresses something more like the abstract notion of vertical 300 pixel black lines Without specifying which vertical 300 pixel black line we mean
25
Programming through abstraction A good way to write simple procedures: Think of what its output should look like in some specific case 1. Write an expression for it 2. Change part of it into a variable 3. Wrap it with [variable → … ] This technique is called abstracting the expression Or, if you want to intimidate your friends, you can call it λ-abstraction 1. [line [point 0 0] [point 0 300]] 2. [line [point n 0] [point n 300]] 3. [n → [line [point n 0] [point n 300]]]
26
Programming through abstraction Alas, it usually isn’t that simple You often have to do something to the input before it’s really in the form you want And often times you have to play with it a bit to make it work the way you really want 1. [line [point 0 0] [point 0 300]] 2. [line [point n 0] [point n 300]] 3. [n → [line [point n 0] [point n 300]]] 4. [n → [line [point [× 20 n] 0] [point [× 20 n] 300]]]
27
Making a radial line pattern How do we make a pattern of lines radiating out from a central point?
28
Making a radial line pattern How do we make one line? [line [point 0 0] [point 0 300]]
29
Making a radial line pattern How do we make one rotated line? [rotate 30 [line [point 0 0] [point 0 300]]]
30
Making a radial line pattern How do we make an arbitrary rotated line? [rotate n [line [point 0 0] [point 0 300]]]
31
Making a radial line pattern How do we make a procedure that makes arbitrary rotated lines? [n → [rotate n [line [point 0 0] [point 0 300]]]]
32
Making a radial line pattern How do we make a lot of rotated lines? [iterated-group [n → [rotate n [line [point 0 0] [point 0 300]]]] 10]
33
Fixing it up How do we make a full circle? [iterated-group [n → [rotate [× 36 n] [line [point 0 0] [point 0 300]]]] 10]
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.