150 likes | 254 Views
Computer Programming. How do you write a computer program? Expressing thoughts as formulas Building complex objects from simple parts Dealing with misteaks. Expressing thoughts as formulas. Everything a computer does must be written down in a highly constrained “language”
E N D
Computer Programming • How do you write a computer program? • Expressing thoughts as formulas • Building complex objects from simple parts • Dealing with misteaks
Expressing thoughts as formulas • Everything a computer does must be written down in a highly constrained “language” • It’s more difficult than drawing a picture • But immense power comes from writing instructions that are general
Complex objects, simple parts • The building blocks for computer programs are few in kind, but can be combined in many ways • Millions of different programs can be built from a fixed number of components • Understanding how the components work is enough to understand any system built from them
Dealing with mistakes • Computers are ruthlessly precise machines; humans less so • In computer programming, mistakes are inevitable • We try to correct all the mistakes before we say that a program is finished
Introducing GeomLab • GeomLab is a computer program that lets you experience what programming is like • A very simple language for describing pictures • Because the results are pictures, you can easily see your mistakes • Inspired by the art of M. C. Escher
Kinds of mistake • Writing something that’s not quite a formula • sqrt(2 + 7 … Oops! • Writing a formula that’s meaningless • sqrt(-2) … Aargh! • Writing the wrong formula • sqrt(b – 4*a*c) … Oh! • Don’t worry about it!
Recurrences and recursion • How can we define a function manrow(n): • manrow(1) is a row of 1 man • manrow(2) is a row of 2 men • manrow(3) is a row of 3 men • … • Capturing an infinite variety of behaviour in a finite definition
Base and step • First, notice that • manrow(1) = man • Then look for a relationship between manrow(n) and manrow(n-1): • manrow(n) = manrow(n-1) $ man • These facts give a base for defining manrow, and a way of stepping from one case to the next
Making a recursive definition • Combine the base and step into a single definition: • define manrow(n) = manrow(n-1) $ man when n > 1 | manrow(1) = man • We call this definition recursive, because it appears to be defining manrow in terms of itself…
Expanding the recursion • …but we can use the definition like this: • manrow(5) = manrow(4) $ man = manrow(3) $ man $ man = manrow(2) $ man $ man $ man = manrow(1) $ man $ man $ man $ man = man $ man $ man $ man $ man • That’s how GeomLab calculates with recursive definitions.
Drawing spirals • How can we define a function that draws more and more complicated spirals? • The key step is to find the relationship between spiral(n) and spiral(n-1)
Building a bigger spiral ? spiral(7) spiral(8)
Building a bigger spiral side(8) 1 x 8 • spiral(8) = side(8) & (rot(side(7)) $ rot2(spiral(7))) spiral(7) rot(side(7))7 x 1 rot2(spiral(7)) 7 x 7
Where do we start? • spiral(1) must be a 1 x 1 picture. • In fact, it works well to define • spiral(1) = bend
Putting it all together • We now have all the information we need: • define spiral(n) = side(n) & (rot(side(n-1)) $ rot2(spiral(n-1))) when n > 1 | spiral(1) = bend • define side(n+1) = side(n) $ straight | side(1) = bend