two motion and change: programming with imperatives

48
two motion and change: programming with imperatives

Upload: maryam-stockdill

Post on 02-Apr-2015

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Two motion and change: programming with imperatives

two

motion and change:programming with imperatives

Page 2: Two motion and change: programming with imperatives

Overview

Review Imperative Programming More on objects Appendix

Page 3: Two motion and change: programming with imperatives

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

Page 4: Two motion and change: programming with imperatives

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

Page 5: Two motion and change: programming with imperatives

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-expression1 through arg-expressionn ) Run the value of, passing it the values of proc-expression , passing it the values

of arg-expression1 through arg-expressionn as inputs Use its output as the value of the expression

These rules are worth memorizing

Page 6: Two motion and change: programming with imperatives

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 name1 = value-expression1

… namelast = value-expressionlastresult-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[name1 … namelast → result-expression]

Make a procedure That names its inputs

name1 … namelast 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

Page 7: Two motion and change: programming with imperatives

Overview

Review Imperative Programming More on objects Appendix

Page 8: Two motion and change: programming with imperatives

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

Page 9: Two motion and change: programming with imperatives

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

Page 10: Two motion and change: programming with imperatives

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

Page 11: Two motion and change: programming with imperatives

Changing a global variable

[define count 0]

[define increment! [→ [count ← [+ count 1]]]]

[define clear! [→ [count ← 0]]]

> count0> [increment!]<NoValue>> count1> [clear!]<NoValue>> count0>

Page 12: Two motion and change: programming with imperatives

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

Page 13: Two motion and change: programming with imperatives

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

Page 14: Two motion and change: programming with imperatives

Changing a global variable

[define count 0]

[define increment! [→ [count ← [+ count 1]] count]]

[define clear! [→ [count ← 0] count]]

> count0> [increment!]1> count1> [clear!]0> count0>

Page 15: Two motion and change: programming with imperatives

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]

Page 16: Two motion and change: programming with imperatives

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

Page 17: Two motion and change: programming with imperatives

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

Page 18: Two motion and change: programming with imperatives

Example: [fold + [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]]]

Var Valueproc +list [1 2 3 4]

Page 19: Two motion and change: programming with imperatives

Example: [fold + [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]]]

Var Valueproc +list [1 2 3 4]answer 1position 1

Page 20: Two motion and change: programming with imperatives

Example: [fold + [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]]]

Var Valueproc +list [1 2 3 4]answer 1position 1

Page 21: Two motion and change: programming with imperatives

Example: [fold + [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]]]

Var Valueproc +list [1 2 3 4]answer 3position 1

Page 22: Two motion and change: programming with imperatives

Example: [fold + [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]]]

Var Valueproc +list [1 2 3 4]answer 3position 2

Page 23: Two motion and change: programming with imperatives

Example: [fold + [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]]]

Var Valueproc +list [1 2 3 4]answer 3position 2

Page 24: Two motion and change: programming with imperatives

Example: [fold + [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]]]

Var Valueproc +list [1 2 3 4]answer 6position 2

Page 25: Two motion and change: programming with imperatives

Example: [fold + [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]]]

Var Valueproc +list [1 2 3 4]answer 6position 3

Page 26: Two motion and change: programming with imperatives

Example: [fold + [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]]]

Var Valueproc +list [1 2 3 4]answer 6position 3

Page 27: Two motion and change: programming with imperatives

Example: [fold + [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]]]

Var Valueproc +list [1 2 3 4]answer 10position 3

Page 28: Two motion and change: programming with imperatives

Example: [fold + [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]]]

Var Valueproc +list [1 2 3 4]answer 10position 4

Page 29: Two motion and change: programming with imperatives

Example: [fold + [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]]]

Var Valueproc +list [1 2 3 4]answer 10position 4

Page 30: Two motion and change: programming with imperatives

Example: [fold + [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]]]

Var Valueproc +list [1 2 3 4]answer 10position 4

Page 31: Two motion and change: programming with imperatives

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

Page 32: Two motion and change: programming with imperatives

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

<Procedure fold>► [fold + [list 1 2 3 4]]Error: Index was outside the bounds of the array.►

Var Valueproc +list [1 2 3 4]answer 8position 4

Page 33: Two motion and change: programming with imperatives

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

<Procedure fold>► [fold + [list 1 2 3 4]]Error: Index was outside the bounds of the array.►

Var Valueproc +list [1 2 3 4]answer 8position 4

Page 34: Two motion and change: programming with imperatives

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

Page 35: Two motion and change: programming with imperatives

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

Page 36: Two motion and change: programming with imperatives

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

Page 37: Two motion and change: programming with imperatives

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

Page 38: Two motion and change: programming with imperatives

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

Page 39: Two motion and change: programming with imperatives

Overview

Review Imperative Programming More on objects Appendix

Page 40: Two motion and change: programming with imperatives

NumberValue: 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

BoxWidth: 10Height: 10

EllipseWidth: 15Height: 10

ProcedureName: iterated-groupArguments: proc countBody: [apply group [up-to count proc]]

ColorR: 240G: 220B: 0

Page 41: Two motion and change: programming with imperatives

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

BoxWidth: 10Height: 10

EllipseWidth: 15Height: 10

ProcedureName: iterated-groupArguments: proc countBody: [apply group [up-to count proc]]

ColorR: 240G: 220B: 0

Page 42: Two motion and change: programming with imperatives

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”]

Page 43: Two motion and change: programming with imperatives

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)

Page 44: Two motion and change: programming with imperatives

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]

Page 45: Two motion and change: programming with imperatives

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.

Page 46: Two motion and change: programming with imperatives

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

Page 47: Two motion and change: programming with imperatives

Overview

Review Imperative Programming More on objects Appendix

Page 48: Two motion and change: programming with imperatives

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