Presentation is loading. Please wait.

Presentation is loading. Please wait.

constructing simple procedures using abstraction

Similar presentations


Presentation on theme: "constructing simple procedures using abstraction"— Presentation transcript:

1 constructing simple procedures using abstraction
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-expression1 … arg-expressionn] Execute all the subexpressions (proc-expression and arg-expression1 through arg-expressionn ) Run the value of, passing it the values of proc-expression , passing it the values of arg-expression1 through arg-expressionn as inputs Use its output as the value of the expression These rules are worth memorizing

3 Special cases If it has a → inside it Make a procedure
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 name1 = value-expression1 … namelast = value-expressionlast 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 [name1 … namelast → result-expression] Make a procedure That names its inputs name1 … namelast 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 procdure 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] + 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 ]]

12 Drawing a line How do we make it two pixels wide?
[line [point 0 0] [point ]

13 Drawing a line How do we make it two pixels wide?
[ink [pen “black” 2] [line [point 0 0] [point ]]

14 Drawing a line How do we make it three pixels wide?
[ink [pen “black” 2] [line [point 0 0] [point ]]

15 Drawing a line How do we make it three pixels wide?
[ink [pen “black” 3] [line [point 0 0] [point ]]

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 ]]

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 ]]]

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 ]]]

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] ] [point [× 20 n] ]]]

22 Theme and variation How do we get a whole series of lines of progressive widths?

23 Theme and variation generates a single line number of lines to make
[iterated-group [n → [ink [pen “black” n] [line [point [× n 20] ] [point [× n 20] ]]]] 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 Write an expression for it Change part of it into a variable Wrap it with [variable → … ] This technique is called abstracting the expression Or, if you want to intimidate your friends, you can call it λ-abstraction [line [point 0 0] [point 0 300]] [line [point n 0] [point n 300]] [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 [line [point 0 0] [point 0 300]] [line [point n 0] [point n 300]] [n → [line [point n 0] [point n 300]]] [n → [line [point [× 20 n] ] [point [× 20 n] ]]]

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 [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]


Download ppt "constructing simple procedures using abstraction"

Similar presentations


Ads by Google