View
217
Download
2
Tags:
Embed Size (px)
Citation preview
five
metaphors in computation
Program as recipe
A program is a description of a computation It’s like a recipe
Tells out to make an output (food) From inputs (ingredients) Through a series of operations (Some of which may be other recipes)
Okay, so what are computations like?
Computation
Computations have inputs and outputs Inputs are also known as arguments or parameters Outputs are also known as results or return values
input output
Computation as call and response
Procedures are “called” with their inputs And “respond” with their return values
input output
Computation as transformation
It’s often useful to think of the computation as transformingthe input into the output Photoshop turns images into brighter/darker/funkier images WinAmp turns the CD representation of sound (sound amplitudes)
into the MP3 representation of sound (sound spectra)
input output
Computation as transformation
Many computations are cascaded transforms Netscape turns
A mouse click into A link to follow, into An HTML file, into Colors for the pixels on the screen
The Pythagorean Theorem
Pythagoras proved The square of the long side of a
right triangle Has the same area as the squares
of the two other sides together
Proof was through geometric construction
Let you “see” the proof But the arguments behind it were
justified by the axioms of geometry
Important in analytic geometry and therefore, in computer graphics
Analytic geometry
Descartes introduced the method of co-ordinates to specify points
Merged geometry with arithmetic
Described space (geometry) Using numbers
(arithmetic)
Allowed theorems from each to be used for the other
(6.27, 4.66)
(8.63, 3.02)
4.66-3.02 =
1.64
8.63-6.27 = 2.36
√(2.362 + 1.642 ) = 2
.87
Computation as flow
Computations can have multiple inputs The patterns they form are more complicated than
simple chains But they’re still networks of data flow
x
y
x
y
−
−
n2
n2
+ √n length
start
end
line
Computation as flow
Computations can have multiple inputs The patterns they form are more complicated than
simple chains But they’re still networks of data flow
x
y
x
y
−
−
n2
n2
+ √n length
start
end
line
0
0
1
1
Computation as flow
Computations can have multiple inputs The patterns they form are more complicated than
simple chains But they’re still networks of data flow
x
y
x
y
−
−
n2
n2
+ √n length
start
end
line
0
0
1
1
1
1
Computation as flow
Computations can have multiple inputs The patterns they form are more complicated than
simple chains But they’re still networks of data flow
x
y
x
y
−
−
n2
n2
+ √n length
start
end
line
0
0
1
1
1
1
1
1
Computation as flow
Computations can have multiple inputs The patterns they form are more complicated than
simple chains But they’re still networks of data flow
x
y
x
y
−
−
n2
n2
+ √n length
start
end
line
0
0
1
1
1
1
1
1
2
Computation as flow
Computations can have multiple inputs The patterns they form are more complicated than
simple chains But they’re still networks of data flow
x
y
x
y
−
−
n2
n2
+ √n length
start
end
line
0
0
1
1
1
1
1
1
2 1.414
Computation as flow
Connection patterns can be tricky One output can feed many inputs A computation’s output can be one of its own inputs
+
delay
sound sound
Computation as sequence
Ultimately, computations are performed as a series of steps
Programs implicitly define the sequence of steps
But we avoid specifying the sequence directly
Because it looses much of the structure of the problem
Get the line’s start point Get its x coordinate Get the line’s end point Get its x coordinate Find the −2 of the coordinates Get the line’s start point Get its y coordinate Get the line’s end point Get its y coordinate Find the −2 of the coordinates Sum the two squared
differences Take the square root
Program as pattern
Programs are descriptions of computations Computations exhibit recurring patterns
Repeated operations in a specific computation Common patterns across computations
x
y
x
y
− n2
− n2
+ √n length
start
end
line
Program as pattern
Much of programming is identifying patterns Programming languages are designed to let you
Name important patterns “Ask for” patterns by name
x
y
x
y
−2
−2
+ √n length
start
end
line
− n2=−2
Program as pattern
Named patterns are called abstractions Procedural abstractions (procedures, subroutines) Data abstractions (types, classes)
x
y
x
y
−2
−2
+ √n length
start
end
line
− n2=−2
Program as pattern
Abstractions can be referred to by name Procedures are called (“invoked”, “applied”) to yield an output Data types are instantiated to yield a data object
Effect is to make a new copy of the pattern
x
y
x
y
−2
−2
+ √n length
start
end
line
− n2=−2
Program as language
Programs aredescriptions of computations
Programs are recipes
Descriptions take many forms Diagrams Equations Numerical codes
But language is the most common form of program
To find the length of a line Sum the −2 of the x- and y-
coordinates of the start and end of the line and take the square root
To find the −2 of a and b Square a minus b
Program as language
Why language? Not the simplest medium for
description Or the most efficient
But Very good at expressing
abstractions Very good at naming Arguably most versatile
To find the length of a line Sum the −2 of the x- and y-
coordinates of the start and end of the line and take the square root
To find the −2 of a and b Square a minus b
Program as grammar
To find the length of a line Sum the −2 of the x- and y-
coordinates of the start and end of the line and take the square root
To find the −2 of a and b Square a minus b
To find the length of a line Sum:
The −2 of The x- and y- coords of
the start and end of the line
And take the square root
To find the −2 of a and b Square:
a minus bLanguage has hierarchical structure
Clauses inside sentences Phrases inside clauses Words inside phrases Syllables inside words
Program as hybrid medium
Reading complicated sentences is hard
Phrases inside phrases inside phrases
Easy to get lost
So programs are traditionally indented like an outline
Makes the structure visual Lets you start reading in the
middle
Programs are visual after all
To find the length of a line Sum:
The −2 of The x- and y- coords of
the start and end of the line
And take the square root
To find the −2 of a and b Square:
a minus b
Computation as substitution
Computation follows the program’s outline structure To compute the output
of a phrase First compute its
subphrases’ outputs
To find the length of a line
Sum: The −2 of
The x- and y- coords of the start and end of
the line
And take the square root
Computation as substitution
Computation follows the program’s outline structure To compute the output
of a phrase First compute its
subphrases’ outputs
To find the length of a line
Sum: The −2 of
The x- and y- coords of the start and end of
(0,0) to (1,1)
And take the square root
Computation as substitution
Computation follows the program’s outline structure To compute the output
of a phrase First compute its
subphrases’ outputs
To find the length of a line
Sum: The −2 of
The x- and y- coords of Start: (0,0) End: (1,1)
And take the square root
Computation as substitution
Computation follows the program’s outline structure To compute the output
of a phrase First compute its
subphrases’ outputs
To find the length of a line
Sum: The −2 of
X coords: 0 and 1 Y coords: 0 and 1
And take the square root
Computation as substitution
Computation follows the program’s outline structure To compute the output
of a phrase First compute its
subphrases’ outputs
To find the length of a line
Sum: The square of
The difference of X coords: 0 and 1 Y coords: 0 and 1
And take the square root
Computation as substitution
Computation follows the program’s outline structure To compute the output
of a phrase First compute its
subphrases’ outputs
To find the length of a line
Sum: The square of
1 and 1
And take the square root
Computation as substitution
Computation follows the program’s outline structure To compute the output
of a phrase First compute its
subphrases’ outputs
To find the length of a line
Sum: 1 and 1
And take the square root
Computation as substitution
Computation follows the program’s outline structure To compute the output
of a phrase First compute its
subphrases’ outputs
To find the length of a line
2 And take the square
root
Computation as substitution
Computation follows the program’s outline structure To compute the output
of a phrase First compute its
subphrases’ outputs
1.414
Computation as substitution
Connection patterns can be tricky One output can feed many inputs A computation’s output can be one of its own inputs
done?
reduce
expression value (object)
no
yes