Seven constructing simple procedures using abstraction.

Post on 21-Dec-2015

215 views 0 download

Tags:

transcript

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

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)

What is the value of this expression?

[+ [× 2 2] 3]

What is the value of this expression?

[+ [× 2 2] 3]

7

What is the value of this expression?

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

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

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

What is the value of this expression?

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

4

The rules are simple, but very subtle

Drawing a line

How do we make a 300 pixel vertical line?

Drawing a line

How do we make a 300 pixel vertical line?

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

Drawing a line

How do we make it two pixels wide?

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

Drawing a line

How do we make it two pixels wide?

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

Drawing a line

How do we make it three pixels wide?

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

Drawing a line

How do we make it three pixels wide?

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

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

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

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

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

Drawing a line

How do we make it shift the line more?

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

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

Theme and variation

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

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

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

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

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

Making a radial line pattern

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

Making a radial line pattern

How do we makeone line?

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

Making a radial line pattern

How do we makeone rotated line?

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

Making a radial line pattern

How do we makean arbitrary rotated line?

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

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

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]

Fixing it up

How do we makea full circle?

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