33
seven constructing simple procedures using abstraction

Seven constructing simple procedures using abstraction

  • View
    215

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Seven constructing simple procedures using abstraction

seven

constructing simple procedures using abstraction

Page 2: Seven constructing simple procedures using abstraction

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

Page 3: Seven constructing simple procedures using abstraction

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 name1 = value-expression1

… namelast = value-expressionlastresult-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)

Page 4: Seven constructing simple procedures using abstraction

What is the value of this expression?

[+ [× 2 2] 3]

Page 5: Seven constructing simple procedures using abstraction

What is the value of this expression?

[+ [× 2 2] 3]

7

Page 6: Seven constructing simple procedures using abstraction

What is the value of this expression?

[ [n → [+ n 1]] 3]

Page 7: Seven constructing simple procedures using abstraction

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

Page 8: Seven constructing simple procedures using abstraction

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

Page 9: Seven constructing simple procedures using abstraction

What is the value of this expression?

[ [n → [+ n 1]] 3]

4

The rules are simple, but very subtle

Page 10: Seven constructing simple procedures using abstraction

Drawing a line

How do we make a 300 pixel vertical line?

Page 11: Seven constructing simple procedures using abstraction

Drawing a line

How do we make a 300 pixel vertical line?

[line [point 0 0] [point 0 300]]

Page 12: Seven constructing simple procedures using abstraction

Drawing a line

How do we make it two pixels wide?

[line [point 0 0] [point 0 300]

Page 13: Seven constructing simple procedures using abstraction

Drawing a line

How do we make it two pixels wide?

[ink [pen “black” 2] [line [point 0 0] [point 0 300]]

Page 14: Seven constructing simple procedures using abstraction

Drawing a line

How do we make it three pixels wide?

[ink [pen “black” 2] [line [point 0 0] [point 0 300]]

Page 15: Seven constructing simple procedures using abstraction

Drawing a line

How do we make it three pixels wide?

[ink [pen “black” 3] [line [point 0 0] [point 0 300]]

Page 16: Seven constructing simple procedures using abstraction

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

Page 17: Seven constructing simple procedures using abstraction

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

Page 18: Seven constructing simple procedures using abstraction

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

Page 19: Seven constructing simple procedures using abstraction

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

Page 20: Seven constructing simple procedures using abstraction

Drawing a line

How do we make it shift the line more?

[n → [ink [pen “black” n] [line [point n 0] [point n 300]]]

Page 21: Seven constructing simple procedures using abstraction

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

Page 22: Seven constructing simple procedures using abstraction

Theme and variation

How do we get a whole series of lines of progressive widths?

Page 23: Seven constructing simple procedures using abstraction

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

Page 24: Seven constructing simple procedures using abstraction

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

Page 25: Seven constructing simple procedures using abstraction

Programming through abstraction

A good way to write simple procedures: Think of what its output should look

like in some specific case1. Write an expression for it2. Change part of it into a variable3. 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]]]

Page 26: Seven constructing simple procedures using abstraction

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

Page 27: Seven constructing simple procedures using abstraction

Making a radial line pattern

How do we make a pattern of lines radiating out from a central point?

Page 28: Seven constructing simple procedures using abstraction

Making a radial line pattern

How do we makeone line?

[line [point 0 0] [point 0 300]]

Page 29: Seven constructing simple procedures using abstraction

Making a radial line pattern

How do we makeone rotated line?

[rotate 30 [line [point 0 0] [point 0 300]]]

Page 30: Seven constructing simple procedures using abstraction

Making a radial line pattern

How do we makean arbitrary rotated line?

[rotate n [line [point 0 0] [point 0 300]]]

Page 31: Seven constructing simple procedures using abstraction

Making a radial line pattern

How do we makea procedure that makes arbitrary rotated lines?

[n → [rotate n [line [point 0 0] [point 0 300]]]]

Page 32: Seven constructing simple procedures using abstraction

Making a radial line pattern

How do we makea lot of rotated lines?

[iterated-group [n → [rotate n [line [point 0 0] [point 0 300]]]] 10]

Page 33: Seven constructing simple procedures using abstraction

Fixing it up

How do we makea full circle?

[iterated-group [n → [rotate [× 36 n] [line [point 0 0] [point 0 300]]]] 10]