Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of...

57
Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21

Transcript of Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of...

Page 1: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Iteration and Recursion

Prepared by

Manuel E. Bermúdez, Ph.D.Associate ProfessorUniversity of Florida

Programming Language PrinciplesLecture 21

Page 2: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Iteration

• Execute a block multiple times for its side effects.

• Enumeration controlled loops:• Execute a block for every value in a

finite set.• Fortran:

do 10 i=1,10,2 ... 10 continue :

Page 3: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Classic Problems in Fortran

• 'do' as prefix of assignment:

do7i=j+3*4,20 do loop do7i=j+3+4 assignment

'do7i' is a valid identifier. 'do' not a reserved word in Fortran. Need to look ahead arbitrary

distance, to the 'comma'.

Page 4: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Classic Problems in Fortran (cont’d)

• Loop body can change the loop index:

do 10 i=2,20 i=i-1 10 continue infinite loop

• Goto's can jump into the middle of a loop. • So what's the value of the index,

in that case ??

Page 5: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Classic Problems in Fortran (cont’d)

• If goto branches out of loop, value of i is last assigned. If loop terminates normally, value of i is implementation-defined.• Last increment of i can cause overflow:

negative value ??? • Fortran loop is bottom-tested: loop body

will execute at least once.

Page 6: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Pascal

• (and Modula, and most languages with enumeration controlled loops):

for i := first to last by step do

begin

...

end;

Page 7: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Pascal (cont’d)

• Questions:1. Can i, first or last be modified in the loop?

• If so, what is the effect?2. What if first > last?3. What 's the value of i when done?4. Can one jump from outside, into the loop?5. What if first has a side effect on i, or on

last, or vice-versa?

Page 8: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Pascal (cont’d)

• Most languages prohibit modifying the loop index.• Very expensive to check at compile

time. Need to check for:• Assignments to i• Nested loops that use i.• Passing i to a procedure by

reference.• Reading i.

Page 9: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Pascal (cont’d)

• What does this Pascal for statement do?

i := 7; for i := i+1 to i+8 do

begin

...

end;

• What if instead of i+1, we had f(i), where

f takes i as a reference parameter ?

Page 10: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Modern For Loops Are Top-Tested

Page 11: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Modern For Loops Are Top-Tested (cont’d)

• Works only for positive steps.

• In Pascal, for i := 10 down to 1 do ...• In Ada, for i in reverse 1..10 do ...• In Modula, FOR i := 10 to 1 STEP -1

compile-time constant

Page 12: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Modern For Loops Are Top-Tested (cont’d)

• In Fortran• No "backward" syntax,• Compile-time constant steps not required.• Can use an "iteration count“

Page 13: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Modern For Loops Are Top-Tested (cont’d)

• In C, C++, Java, it's simple. No loop index.

for (e1; e2; e3) body; Code is:

e1;L2: if not e2 goto L1 body e3; <--- any 'continue' branches here goto L2L1: ...

Page 14: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Modern For Loops Are Top-Tested (cont’d)

• NOT equivalent to:

e1; while (e2) { body; /* if this contains a 'continue', */ e3; /* e3 is not executed */

}

Page 15: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Modern For Loops Are Top-Tested (cont’d)

• In C, programmer's responsibility:

• effect of overflow.• index, other variables, can be

modified inside loop.• e1, e2, e3 are all optional. If e2 is

missing, it's considered to be a 1 (true).

Page 16: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Access to the Index Outside the Loop

• Fortran IV, Pascal, leave the loop index undefined.

• Fortran 77, Algol 60, leave the "last value assigned".

• In Pascal,

var c: 'a' .. 'z'; for c := 'a' to 'z' do begin ... end; (* what's the value of c ? *)

Page 17: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Access to the Index Outside the Loop (cont’d)

• Compiler forced to generate slower code

• Two branches in each iteration

Page 18: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Access to the Index Outside the Loop (cont’d)

• Several languages (Algol W, Algol 68, Ada, Modula-3, new ISO C++):

• loop header *declares* loop index.• loop index's type is inferred from

loop bounds.• not visible outside the loop.

Page 19: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Combination Loops

• Algol 60 allows a 'for-list' of enumerated values/ranges.

• Syntax:

Stmt 'for' id ':=‘ Enumerator list ',' 'do' Stmt

Enumerator Expr

Expr 'step' Expr 'until' Expr

Expr while Condition

Page 20: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Combination Loops (cont’d)

• Examples (all three are equivalent):

for i := 1, 3, 5, 9 do ... for i := 1 step 2 until 10 do ...

for i := 1, i + 2 while i < 10 do ...

Page 21: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Iterators

• An iterator allows examination of the elements in a data structure, one at a time.

• Various kinds of iterators available in Clu, Icon (see textbook).

• Java has a built-in iterator class.

Page 22: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Iterator Methods in Java

Iterator ix = x.iterator();

• Constructs and initializes an iterator for the elements of x.

• ix is the new iterator.

• The class for x must define the iterator() method.

Page 23: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Iterator Methods in Java (cont’d)

ix.hasNext()

• Returns true iff x has a next element.

ix.next()

• Return next element;• Throws NoSuchElementException if

there is no next element.

Page 24: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Iterator Methods in Java (cont’d)

ix.remove()

• Removes last element returned by ix.next().

• Throws UnsupportedMethodEXception if method not implemented.

• Throws IllegalStateException if ix.next() not yet called or did not return an element.

Page 25: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Using the Iterator

Iterator ix = x.iterator();

while (ix.hasNext())

examine(ix.next());

• Much better than

for (int i=0; i<x.size(); i++) examine (x.get(i));

Page 26: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Advantages of Iterators

• More abstraction. Object class "knows" how to iterate.

• Often possible to implement next() more efficiently than get(). Example: linked list.

• Data structures often have no get-by-index method.

Page 27: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

"Faking" an Iterator in C

• Using a "fake" iterator in C:

• C code for the binary tree pre-order traversal iterator (see textbook).

Page 28: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Logically Controlled Loops

• Semantically less complex (fewer subtleties).

• Execute statement (or block) until a condition becomes true/false (while)

• Variations (syntactic sugar of each other):

Page 29: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Logically Controlled Loops (cont’d)

while condition do block; (Pascal, Modula) while (condition) block; (C, C++, Java)for i := irrelevant_expression

while condition do statement (Algol)

10 if negated-condition goto 20

block

goto 10

20 (Fortran)

Page 30: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Logically Controlled Loops (cont’d)

• Post-tested loops (execute at least once):

repeat statements until condition (Pascal)

do statement while condition (C, C++, Java)

Page 31: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Mid-Tested Loops (Quit Anytime):

loop statement_list when condition exit statement_list when condition exit ...

end (Modula-1)

• 'exit' is built in, along with 'when', so exiting from a nested construct is impossible.

Page 32: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Modula-2

• Favored a simple EXIT Statement

LOOP

line = ReadLine; IF AllBlanks(line) THEN EXIT END; ConsumeLine(line) END;

• EXIT statements are only allowed to appear inside LOOPs.• Difficult to enforce.

Page 33: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Modula-3

• EXIT can abort a WHILE, REPEAT, or FOR loop.

C:

for (;;) { line = read_line(stdin); if (all_blanks(line)) break; consume_line (line); }

Page 34: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

C Loops

• [M. Scott says "for some reason, for(;;) has traditionally been favored over the equivalent while (1)".

• The actual original reason was efficiency, not style:• older compilers would actually test the 1 each time around the while loop.

• With today's compilers, it should make no difference.

Page 35: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Mid-Tested Loops (cont’d):

• Euclid, Ada:

loop ... exit when condition; end loop;

Page 36: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Mid-Tested Loops (cont’d):

• Java:• loops can be labeled,• 'break' statement has optional label.

outer: while (true) { get_line(line); for (i=1; i<=length; i++) if line[i]='#' break outer; consume_line(line); }

Page 37: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Recursion

• Any iterative formulation can be expressed as recursion, and vice versa. They are equally powerful.

• Some functional languages do not allow iteration.

• Iteration often more efficiently implemented.

Page 38: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Recursion (cont’d)

• Optimizing compilers for functional languages usually generate very good code.

• The use of iteration/recursion is mostly a matter of ease of use:

• Iteration more efficient than recursion ?• Naive implementations are.

Page 39: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Recursion (cont’d)

• For some problems, iteration seems natural.

for (i=low; i<=high; i++) total += f(i);

• For other problems, recursion seems more natural:

let gcd a b = a eq b -> a | a < b -> gcd a (b-a) | gcd (a-b) b

Page 40: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Recursion (cont’d)

• In C,

int gcd (int a, int b) { if (a==b) return a; else if (a > b) return gcd(a-b,b); else return gcd(a, b-a); }

• In both cases, the choice could go the other way.

Page 41: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Recursion (cont’d)

• Summation in C, recursive:

typedef int (*int_func) int;

int summation (int_func f, int low, int high) {

if (low == high) return f (low)

else return f(low) + summation(f, low+1,high);

}

Page 42: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Recursion (cont’d)

• GCD in C, non-recursive:

int gcd(int a,int b) {

while(a!=b) if (a > b) a = a - b; else b = b - a; return a; }

Page 43: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Tail-Recursion

• Additional computation never follows a recursive call. (nothing done as recursion unwinds).

• No need to allocate stack space dynamically: we can reuse previous space.

• Continuation-passing style: • can always avoid doing work after

recursive call by passing that work into the recursive call, a continuation.

Page 44: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Tail-Recursion (cont’d)

• Example (RPAL):

let rec f n = n eq 1 -> 1 | n * f (n-1) in f 3

let f n = rf n 1 where

rec rf n r = n eq 1 -> r | rf (n-1) (n*r)in f 3

Page 45: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Tail-Recursion in Scheme

Page 46: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Tail-Recursion (cont’d)

• Recursion does not lead to algorithmically inferior programs.

• Instead, the style of programming just changes (paradigmatically).

Page 47: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Fibonacci tail-recursion (Scheme vs. C)

Page 48: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Applicative and Normal-Order Evaluation

• Called Normal Order and PL Order in RPAL.

• Normal order (passing unevaluated parameters) is used for macros in C.

Page 49: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Applicative and Normal-Order Evaluation (cont’d)

• Example:

#define DIVIDES(n,a) (!((n) % (a))) /* true iff n % a is zero */

• Preprocessor replaces

DIVIDES (x,y+z)

(textually!, no evaluation) with

(!((x) % (y+z)))

Page 50: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Applicative and Normal-Order Evaluation (cont’d)

• Parentheses (n), (a) are crucial. Without them,

DIVIDES (x,y+z)

is replaced with (!(x % y+z))

which is equivalent to

(!((x % y)+z))

Page 51: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Applicative and Normal-Order Evaluation (cont’d)

• Macros are problematic:

#define MAX(a,b) ((a) > (b) ? (a) : (b)

MAX(x++, y++)

will increment either x (or y) TWICE !

Page 52: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Applicative and Normal-Order Evaluation (cont’d)

• Advantage of macros:

• Efficient ! No function call overhead.

• In C++, 'inline' tells compiler to try to convert function call to a macro.

• Algol 60 uses normal order, with PL order also available.

Page 53: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Non-Determinacy

• Construct in which the choice between alternatives is deliberately unspecified.

• Example:

n + n++ in C

(order of evaluation unspecified)

Page 54: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Non-Determinacy (cont’d)

• Sometimes makes it easier to reason about program.

• Mostly a set of guarded commands from which any guard that is true can be selected.

Page 55: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Non-Determinacy (cont’d)

• Example (SR, adopted Dykstra's guarded command):

if a >= b -> max := a; [] b >= a -> max := b; fi

• Nondeterministic choice made among guards that evaluate to true.

Page 56: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Non-Determinacy (cont’d)

• Also used for loops:

do a > b -> a := a - b; [] b > a -> b := b - a; od gcd := a

• Loop quits when no guard is true.• We wish to ensure fairness: each candidate

equally likely to execute.

Page 57: Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.

Iteration and Recursion

Prepared by

Manuel E. Bermúdez, Ph.D.Associate ProfessorUniversity of Florida

Programming Language PrinciplesLecture 21