Presentation is loading. Please wait.

Presentation is loading. Please wait.

Two motion and change: programming with imperatives.

Similar presentations


Presentation on theme: "Two motion and change: programming with imperatives."— 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-expression 1 … arg-expression n ] Execute all the subexpressions (proc-expression and arg-expression 1 through arg-expression n ) Run the value of, passing it the values of proc-expression, passing it the values of arg-expression 1 through arg-expression n as inputs Use its output as the value of the expression These rules are worth memorizing

6 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 name 1 = value-expression 1 … name last = value-expression last result-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 [name 1 … name last → result-expression] Make a procedure That names its inputs name 1 … name last And returns the value of 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

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 Changing a global variable [define count 0] [define increment! [→ [count ← [+ count 1]]]] [define clear! [→ [count ← 0]]] > count 0 > [increment!] > count 1 > [clear!] > count 0 >

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 Changing a global variable [define count 0] [define increment! [→ [count ← [+ count 1]] count]] [define clear! [→ [count ← 0] count]] > count 0 > [increment!] 1 > count 1 > [clear!] 0 > count 0 >

15 Iteration (aka looping) so far 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]

16 Looping as a sequencing primitive 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

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 ]] [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]]] VarValue proc+ list[ ]

19 Example: [fold + [list ]] [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]]] VarValue proc+ list[ ] answer1 position1

20 Example: [fold + [list ]] [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]]] VarValue proc+ list[ ] answer1 position1

21 Example: [fold + [list ]] [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]]] VarValue proc+ list[ ] answer3 position1

22 Example: [fold + [list ]] [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]]] VarValue proc+ list[ ] answer3 position2

23 Example: [fold + [list ]] [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]]] VarValue proc+ list[ ] answer3 position2

24 Example: [fold + [list ]] [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]]] VarValue proc+ list[ ] answer6 position2

25 Example: [fold + [list ]] [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]]] VarValue proc+ list[ ] answer6 position3

26 Example: [fold + [list ]] [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]]] VarValue proc+ list[ ] answer6 position3

27 Example: [fold + [list ]] [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]]] VarValue proc+ list[ ] answer10 position3

28 Example: [fold + [list ]] [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]]] VarValue proc+ list[ ] answer10 position4

29 Example: [fold + [list ]] [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]]] VarValue proc+ list[ ] answer10 position4

30 Example: [fold + [list ]] [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]]] VarValue proc+ list[ ] answer10 position4

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]]] ► [fold + [list ]] Error: Index was outside the bounds of the array. ►

32 What happened? ► [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]]] ► [fold + [list ]] Error: Index was outside the bounds of the array. ► VarValue proc+ list[ ] answer8 position4

33 What happened? ► [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]]] ► [fold + [list ]] Error: Index was outside the bounds of the array. ► VarValue proc+ list[ ] answer8 position4

34 Functional programming versus 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 programming versus 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 Fold in functional an imperative form 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]]]

37 Fold in functional an imperative form 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

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 Number Value: 10 Looking inside data objects 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 Ellipse Width: 15 Height: 10 Procedure Name: iterated-group Arguments: proc count Body: [apply group [up-to count proc]] Color R: 240 G: 220 B: 0

41 Number Value: 10 Member notation 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 Ellipse Width: 15 Height: 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 Member procedures (aka methods) 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.member arg … 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)

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 x 1 y 1 x 2 y 2 ]

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 ]] [g.DrawLine [pen “blue” 3] [point 50 0] [point ]] [g.DrawEllipse [pen “red” 40] ]]]

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


Download ppt "Two motion and change: programming with imperatives."

Similar presentations


Ads by Google