1 / 48

two

two. motion and change: programming with imperatives. Overview. Review Imperative Programming More on objects Appendix. The story up until now. Everything in your computer is data Including programs Data is divided into objects Objects can be “inside” of other objects

quang
Download Presentation

two

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. two motion and change:programming with imperatives

  2. Overview • Review • Imperative Programming • More on objects • Appendix

  3. The story up until now • Everything in your computer is data • Including programs • Data is divided into objects • Objects can be “inside” of other objects • Boxes inside groups • Colors inside bitmaps • Objects have types • Procedures • Numbers (1, -3.5) • Strings (“this is a string”, “blue”) • Bitmaps • Picture objects (lines, groups, boxes, etc.)

  4. The story up until now • Computation is performed using expressions • Expressions have (or “return”) values (i.e. outputs) • Computation is performed by recursively replacing expressions with their values • A computation’s only output is its return value • It’s return value depends on • The expression’s structure • The other definitions in the program • It doesn’t depend on what was computed before

  5. 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-expression1through arg-expressionn ) • Run the value of, passing it the values of proc-expression , passing it the values of arg-expression1through arg-expressionn as inputs • Use its output as the value of the expression These rules are worth memorizing

  6. If it has a → inside it [name1 … namelast→result-expression] Make a procedure That names its inputsname1 … namelast And returns the valueof result-expression(presumably using those names) If it starts with if [if test-expression result-expression alternative-expression] Run test-expression If it returns true Run result-expression and return its value Otherwise, Run alternative-expression and return its value Special cases If it starts with define [define name value-expression] • Run value-expression • Assign its value to namein 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 respectivenames in the dictionary • Run result-expression • Set the dictionary back the way it was • Return the value from result-expression

  7. Overview • Review • Imperative Programming • More on objects • Appendix

  8. Change and effect • But some expressions don’t really return values • [define name value]Happens to print out value when you run it, but that’s not the point. • [using package] (e.g.: [using Examples.Stacking]) Returns something cryptic but irrelevant; what matters is that it causes a bunch of procedures to become defined • These are examples of expressions we type • Not because they return values • But because they do things • They change the computer • These changes are called side effects, or just effects • What it really means is change caused by the expression

  9. Calling procedures for their effects • You can write procedures that make a wide range of changes in the computer • Changing a variable’s value (aka assignment) • Changing a data object (aka mutation) • Creating files • Creating windows • Performing input or output • As with everything else in this class, • Complex effects • Are ultimately built up from a few kinds of simple effects • And methods for combining them

  10. Assignment statements • The simplest change primitive is “←” • [name ← new-value] • After execution, the variable name is changed to have the value new-value • Variable must have already been “created” using define, with, or • Why is this different from define? • Define declares an entirely new variable, it doesn’t change existing variables • Okay, I know sometimes it does change existing variables,but that’s just a kluge • We’ll see the difference in a moment

  11. [define count 0] [define increment! [→[count ← [+ count 1]]]] [define clear! [→[count ← 0]]] > count 0 > [increment!] <NoValue> > count 1 > [clear!] <NoValue> > count 0 > Changing a global variable

  12. Sequencing • Changes are most useful when we can chain them together • That means we need some way of specifying that • We want to do several things in a row • And we want them done in a specific order

  13. Sequencing with procedures • Procedures can specify a series of expressions to run • [args …→expression … expression] • [define [name args …]expression … expression] • The expressions are run in order, first to last • Their return values are ignored • Except for the last expression • Procedure’s return value is the value of the last expression

  14. [define count 0] [define increment! [→ [count ← [+ count 1]]count]] [define clear! [→ [count ← 0]count]] > count 0 > [increment!] 1 > count 1 > [clear!] 0 > count 0 > Changing a global variable

  15. So far, when we’ve wanted to do something repeatedly, we’ve Written the something as a procedure Call another procedure that iterates and passed our procedure to it as an argument So forms of iteration are represented by specialized procedures [filter beatles? list] [map album-title [filter beatles? list]] [fold + list] [iterated-group [n → [box [× n 2] [× n 2]]] 10] Iteration (aka looping) so far

  16. Most imperative languages have special constructs for iteration The most basic is the while loop [while test expressions …] Means: Run test If it’s true, run expressions And run test again, etc, Keep going until test is false Looping as a sequencing primitive

  17. Fold in imperative form [define fold [proc list →«Make some variables» [with answer = [first list] position = 1 «Loop over all the elements of the list» [while [< position [length list]]«Fold in one more element» [answer ← [proc answer [get list position]]] [position ← [+ position 1]]] «Return the accumulated answer» answer]]]

  18. Example: [fold + [list 1 2 3 4]] Var Value proc + list [1 2 3 4] [define fold [proc list →«Make some variables» [with answer = [first list] position = 1 «Loop over all the elements of the list» [while [< position [length list]]«Fold in one more element» [answer ← [proc answer [get list position]]] [position ← [+ position 1]]] «Return the accumulated answer» answer]]]

  19. Example: [fold + [list 1 2 3 4]] Var Value proc + list [1 2 3 4] answer 1 position 1 [define fold [proc list →«Make some variables» [with answer = [first list] position = 1 «Loop over all the elements of the list» [while [< position [length list]]«Fold in one more element» [answer ← [proc answer [get list position]]] [position ← [+ position 1]]] «Return the accumulated answer» answer]]]

  20. Example: [fold + [list 1 2 3 4]] Var Value proc + list [1 2 3 4] answer 1 position 1 [define fold [proc list →«Make some variables» [with answer = [first list] position = 1 «Loop over all the elements of the list» [while [< position [length list]]«Fold in one more element» [answer ← [proc answer [get list position]]] [position ← [+ position 1]]] «Return the accumulated answer» answer]]]

  21. Example: [fold + [list 1 2 3 4]] Var Value proc + list [1 2 3 4] answer 3 position 1 [define fold [proc list →«Make some variables» [with answer = [first list] position = 1 «Loop over all the elements of the list» [while [< position [length list]]«Fold in one more element» [answer ← [proc answer [get list position]]] [position ← [+ position 1]]] «Return the accumulated answer» answer]]]

  22. Example: [fold + [list 1 2 3 4]] Var Value proc + list [1 2 3 4] answer 3 position 2 [define fold [proc list →«Make some variables» [with answer = [first list] position = 1 «Loop over all the elements of the list» [while [< position [length list]]«Fold in one more element» [answer ← [proc answer [get list position]]] [position ← [+ position 1]]] «Return the accumulated answer» answer]]]

  23. Example: [fold + [list 1 2 3 4]] Var Value proc + list [1 2 3 4] answer 3 position 2 [define fold [proc list →«Make some variables» [with answer = [first list] position = 1 «Loop over all the elements of the list» [while [< position [length list]]«Fold in one more element» [answer ← [proc answer [get list position]]] [position ← [+ position 1]]] «Return the accumulated answer» answer]]]

  24. Example: [fold + [list 1 2 3 4]] Var Value proc + list [1 2 3 4] answer 6 position 2 [define fold [proc list →«Make some variables» [with answer = [first list] position = 1 «Loop over all the elements of the list» [while [< position [length list]]«Fold in one more element» [answer ← [proc answer [get list position]]] [position ← [+ position 1]]] «Return the accumulated answer» answer]]]

  25. Example: [fold + [list 1 2 3 4]] Var Value proc + list [1 2 3 4] answer 6 position 3 [define fold [proc list →«Make some variables» [with answer = [first list] position = 1 «Loop over all the elements of the list» [while [< position [length list]]«Fold in one more element» [answer ← [proc answer [get list position]]] [position ← [+ position 1]]] «Return the accumulated answer» answer]]]

  26. Example: [fold + [list 1 2 3 4]] Var Value proc + list [1 2 3 4] answer 6 position 3 [define fold [proc list →«Make some variables» [with answer = [first list] position = 1 «Loop over all the elements of the list» [while [< position [length list]]«Fold in one more element» [answer ← [proc answer [get list position]]] [position ← [+ position 1]]] «Return the accumulated answer» answer]]]

  27. Example: [fold + [list 1 2 3 4]] Var Value proc + list [1 2 3 4] answer 10 position 3 [define fold [proc list →«Make some variables» [with answer = [first list] position = 1 «Loop over all the elements of the list» [while [< position [length list]]«Fold in one more element» [answer ← [proc answer [get list position]]] [position ← [+ position 1]]] «Return the accumulated answer» answer]]]

  28. Example: [fold + [list 1 2 3 4]] Var Value proc + list [1 2 3 4] answer 10 position 4 [define fold [proc list →«Make some variables» [with answer = [first list] position = 1 «Loop over all the elements of the list» [while [< position [length list]]«Fold in one more element» [answer ← [proc answer [get list position]]] [position ← [+ position 1]]] «Return the accumulated answer» answer]]]

  29. Example: [fold + [list 1 2 3 4]] Var Value proc + list [1 2 3 4] answer 10 position 4 [define fold [proc list →«Make some variables» [with answer = [first list] position = 1 «Loop over all the elements of the list» [while [< position [length list]]«Fold in one more element» [answer ← [proc answer [get list position]]] [position ← [+ position 1]]] «Return the accumulated answer» answer]]]

  30. Example: [fold + [list 1 2 3 4]] Var Value proc + list [1 2 3 4] answer 10 position 4 [define fold [proc list →«Make some variables» [with answer = [first list] position = 1 «Loop over all the elements of the list» [while [< position [length list]]«Fold in one more element» [answer ← [proc answer [get list position]]] [position ← [+ position 1]]] «Return the accumulated answer» answer]]]

  31. Programming with effects can be tricky ► [define fold [proc list →«Make some variables» [with answer = [first list] position = 1 «Loop over all the elements of the list» [while [< position [length list]]«Fold in one more element» [position ← [+ position 1]] [answer ← [proc answer [get list position]]]] «Return the accumulated answer» answer]]] <Procedure fold> ► [fold + [list 1 2 3 4]] Error: Index was outside the bounds of the array. ►

  32. What happened? Var Value proc + list [1 2 3 4] answer 8 position 4 ► [define fold [proc list →«Make some variables» [with answer = [first list] position = 1 «Loop over all the elements of the list» [while [< position [length list]]«Fold in one more element» [position ← [+ position 1]] [answer ← [proc answer [get list position]]]] «Return the accumulated answer» answer]]] <Procedure fold> ► [fold + [list 1 2 3 4]] Error: Index was outside the bounds of the array. ►

  33. What happened? Var Value proc + list [1 2 3 4] answer 8 position 4 ► [define fold [proc list →«Make some variables» [with answer = [first list] position = 1 «Loop over all the elements of the list» [while [< position [length list]]«Fold in one more element» [position ← [+ position 1]] [answer ← [proc answer [get list position]]]] «Return the accumulated answer» answer]]] <Procedure fold> ► [fold + [list 1 2 3 4]] Error: Index was outside the bounds of the array. ►

  34. Functional programmingversus imperative programming • Functional programming is programming without effects and sequencing • Value of a procedure call is determined entirely by the procedure’s arguments • Value of an expression depends only on the computations involved in computing its arguments • Imperative programming • Value of a procedure call can potentially be changed by any of the preceding steps of the computation • Even if they seem unrelated

  35. Functional programmingversus imperative programming • Functional programming is programming without effects and sequencing • Only output from a procedure is its return value • Procedures behave like clauses in English (or functions in math) • Computation is achieved by nesting procedure calls • We think about execution in terms of call and response, transformation, and the other metaphors we discussed last quarter • Imperative programming • Output of a procedure is its effect on the computer • Computation is achieved by sequencing effects • We think about execution in terms of changes and motion

  36. Functional version: [define fold [proc list → [if [= [length list] 1] [first list] [proc [first list] [fold proc [rest list]]]]]] Notes: The functional version uses recursion(remember recursion?) And it uses the rest procedure, which returns all but the first element of a list Also, these two versions don’t do exactly the same thing They process the elements of the list in opposite orders But they’re easier to understand this way Imperative version: [define fold[proc list → [with answer = [first list] position = 1 [while [< position [length list]] [answer ← [proc answer [get list position]]] [position ← [+ position 1]]] answer]]] Fold in functional an imperative form

  37. Functional version: [define fold [proc list → [if [= [length list] 1] [first list] [proc [first list] [fold proc [rest list]]]]]] More focused on what to compute(at least some people think so) Imperative version: [define fold[proc list → [with answer = [first list] position = 1 [while [< position [length list]] [answer ← [proc answer [get list position]]] [position ← [+ position 1]]] answer]]] More focused on how to compute it Fold in functional an imperative form

  38. Advantages of imperative programming • Imperative programs can be more efficient than functional programs • Sometimes it really is simpler • Simulations • What you’re computing just is a series of changes • The changes the simulated system would have made • Imperative style is much more natural • Directly expresses change • Example: video games • Using random numbers • If your random number procedure always returns the same value, it isn’t very useful • Other applications where the task definition involves change

  39. Overview • Review • Imperative Programming • More on objects • Appendix

  40. Looking inside data objects Ellipse Width: 15 Height: 10 • Data objects are like forms • They have fields (aka members) • Filled in by values • The fields • Have names (Width, Height) • The fields are filled in by other data objects • The object’s type (Box, Color) determines what fields it has Box Width: 10 Height: 10 Number Value: 10 Procedure Name: iterated-group Arguments: proc count Body: [apply group [up-to count proc]] Color R: 240 G: 220 B: 0

  41. Member notation Ellipse Width: 15 Height: 10 • You can ask for a field of a data object using the “.” notation: object.memberName • myColor.R • [pixel myBitmap 0 0].R • iterated-group.Name • iterated-group.Arguments • mybox.Width • Note: to simplify the presentation, I’ve lied here about what the actual fields of boxes, procedures, etc. are. • You can find out what fields are really in an object using the inspector. Box Width: 10 Height: 10 Number Value: 10 Procedure Name: iterated-group Arguments: proc count Body: [apply group [up-to count proc]] Color R: 240 G: 220 B: 0

  42. Generalized assignment (aka mutation) • You can also change fields of an object using “←” • [object.member-name ← new-value] • After execution, the field member-name of object is changed to have the value new-value • Object may be any expression (not just a variable) • Examples • [myColor.R ← 7] • [myPen.Brush.Color.R ← 7] • [form.Text ← “This is a the title of this window”]

  43. A lot of procedures are stored “inside” of objects You access them like any other members, except they’re procedures so you can call them [object.memberarg … arg] Examples [someobject.ToString]Converts someobject into a string that (hopefully) is descriptive of the object. [Directory.GetFiles “c:/”]Returns a list of all the files in the specified directory (C:\, in this case) Member procedures (aka methods)

  44. Example: windows graphics calls • All things you can draw on in MS Windows are objects of type “Graphics” • The have many members, but here are some useful methods: • [object.DrawLine pen start end] • [object.DrawEllipse pen x1 y1 x2 y2]

  45. Some magic for making a window [define with-window[name proc → [with w = [new Form] [w.Text ← name] [w.Show] [proc [w.CreateGraphics]] [Application.Run w]]]] As with iterated-group, when we first gave it to you, you don’t yet know enough to understand what this is doing, so don’t sweat it.

  46. Imperative drawing using methods [with-window “test”[g → [g.DrawLine [pen “black” 1] [point 0 0] [point 100 100]] [g.DrawLine [pen “blue” 3] [point 50 0] [point 50 100]] [g.DrawEllipse [pen “red” 40] 0 0 100 100]]]

  47. Overview • Review • Imperative Programming • More on objects • Appendix

  48. begin expressions • Another way of sequencing effects • [begin expression …] • Execute each expression, in order • Again, all return values are ignored • Except for the last • Which is returned as the value of the begin expression

More Related