1 / 15

Computer Programming

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”

donal
Télécharger la présentation

Computer Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Programming • How do you write a computer program? • Expressing thoughts as formulas • Building complex objects from simple parts • Dealing with misteaks

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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!

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

  8. 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

  9. 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…

  10. 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.

  11. 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)

  12. Building a bigger spiral ? spiral(7) spiral(8)

  13. 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

  14. Where do we start? • spiral(1) must be a 1 x 1 picture. • In fact, it works well to define • spiral(1) = bend

  15. 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

More Related