Iteration - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

Iteration

Description:

Thus far, we have achieved repetitive processing by way of recursion ... (define (fib-iter n) (let ((nm2 1) (nm1 1)) (do ((i 2 ( i 1)) (value 1 ( nm2 nm1) ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 31
Provided by: BillL161
Category:
Tags: iteration | nm1

less

Transcript and Presenter's Notes

Title: Iteration


1
Iteration
2
Outline
  • Prerequisites
  • Basic Recursion
  • Processing Lists
  • Objectives
  • Iteration Concepts
  • (do )
  • (let )
  • (set! )
  • Example Iterative Fibonacci

3
Background
  • Thus far, we have achieved repetitive processing
    by way of recursion
  • Determine a terminating condition
  • Call the function repetitively
  • Step towards the terminating condition
  • Example summing a list of numbers
  • (define (sum lst)
  • (if (empty? lst)
  • 0
  • ( (first lst) (sum (rest lst)))))

4
Iteration
  • Used extensively in procedural and
    object-oriented languages
  • Also has three components
  • Determine a terminating condition
  • Use an index to manage the repetition
  • Step towards the terminating condition
  • Example summing numbers from 1 to n
  • sum number -gt number
  • total the numbers from i to n
  • (define (sum n)
  • (do ((i 1 ( i 1))
  • (sum 0 ( sum i)))
  • ((gt i n) sum)))

5
General Form of (do )
  • (do ((ltvariable1gt ltinit1gt ltstep1gt)
  • ...)
  • (lttestgt ltexpressiongt ...)
  • ltcommandgt ...)
  • (do ) specifies
  • a set of variables to be bound,
  • how they are to be initialized at the start, and
  • how they are to be updated on each iteration.
  • When a termination condition is met, the loop
    exits after evaluating the ltexpressiongts.
  • (do ) expressions are evaluated as follows
  • The ltinitgt expressions are evaluated (in some
    order),
  • the results of the ltinitgt expressions are stored
    in the bindings of the ltvariablegts, and then
  • the iteration phase begins.

6
General Form of (do ) continued
  • (do ((ltvariable1gt ltinit1gt ltstep1gt)
  • ...)
  • (lttestgt ltexpressiongt ...)
  • ltcommandgt ...)
  • Each iteration begins by evaluating lttestgt
  • if the result is false, the ltcommandgt expressions
    are evaluated in order for effect,
  • the ltstepgt expressions are evaluated in some
    unspecified order,
  • the ltvariablegts are bound to the results of the
    ltstepgts, and
  • the next iteration begins.

7
General Form of (do ) continued
  • (do ((ltvariable1gt ltinit1gt ltstep1gt)
  • ...)
  • (lttestgt ltexpressiongt ...)
  • ltcommandgt ...)
  • If lttestgt evaluates to a true value, then
  • the ltexpressiongts are evaluated from left to
    right and
  • the value(s) of the last ltexpressiongt is(are)
    returned.
  • If no ltexpressiongts are present, then the value
    of the do expression is unspecified.
  • The region of the binding of a ltvariablegt
    consists of the entire do expression except for
    the ltinitgts. It is an error for a ltvariablegt to
    appear more than once in the list of do
    variables.
  • A ltstepgt may be omitted, in which case the effect
    is the same as if (ltvariablegt ltinitgt ltvariablegt)
    had been written instead of (ltvariablegt ltinitgt).

8
iteration.scm
9
Code Used
  • sum number -gt number
  • total the numbers from i to n
  • (define (sum n)
  • (do ((i 1 ( i 1))
  • (sum 0 ( sum i)))
  • ((gt i n) sum)
  • no body required
  • ))
  • Tests
  • (sum 3) -gt 6
  • (sum 6) -gt 21
  • (sum 100) -gt 5050

10
Questions?
11
The use of (let )
  • Pure functional programming does not permit
    persistence (remembering stuff) in any form
  • This is nice, but there are times when you don't
    want to redo an expensive computation
  • Consider this fragment from the fractal exercise
    earlier
  • (let ((pc (new-location pa pb center radius)))
  • (and (chord pa pc center radius
    color)
  • (chord pc pb center radius
    color))))
  • In this case, it took some effort to compute the
    new location referred to as pc.

12
The use of (let )
  • Without (let ), we would have had to do
  • (and (chord pa
  • (new-location pa pb center radius)
  • center
  • radius
  • color)
  • (chord (new-location pa pb center radius)
  • pb
  • center
  • radius
  • color))
  • In summary, (let ) allows us to remember
    results within a function and re-use them as
    necessary

13
The use of (set! )
  • Just as (let ) allows us to save values we do
    not want to recompute,
  • (set! ) allows us to replace values in
    iterative calculations
  • Example
  • (define (test from to)
  • (let ((x from))
  • (begin
  • (display "x was ") (display x) (newline)
  • (set! x to)
  • (display "x is now ") (display x))))
  • Note test with (1 2), ('fred 'joe), (list
    cons)

14
Iterative Solution to Fibonacci
  • Now that we have some ability to save values,
    let's re-think Fibonacci.
  • (fib n) is computed as
  • (fib (n-1) (fib (n-2))
  • Suppose we did this iteratively starting at 2,
    saving the previous values of fib as we went

15
Iterative Fibonacci
N is 8
I is 2
Fib-2 1
Fib-1 1
Value 1
no
I gt N?
Increase I Move data up Value fib-2 fib-1
yes
Return Value
16
Iterative Fibonacci
N is 8
I is 3
Fib-2 1
Fib-1 1
Value 1
no
I gt N?
Increase I Move data up Value fib-2 fib-1
yes
Return Value
17
Iterative Fibonacci
N is 8
I is 3
Fib-2 1
Fib-1 1
Value 1
no
I gt N?
Increase I Move data up Value fib-2 fib-1
yes
Return Value
18
Iterative Fibonacci
N is 8
I is 3
Fib-2 1
Fib-1 1
Value 1
no
I gt N?
Increase I Move data up Value fib-2 fib-1
yes
Return Value
19
Iterative Fibonacci
N is 8
I is 3
Fib-2 1
Fib-1 1
Value 2
no
I gt N?
Increase I Move data up Value fib-2 fib-1
yes
Return Value
20
Iterative Fibonacci
N is 8
I is 3
Fib-2 1
Fib-1 1
Value 2
no
I gt N?
Increase I Move data up Value fib-2 fib-1
yes
Return Value
21
Iterative Fibonacci
N is 8
I is 4
Fib-2 1
Fib-1 2
Value 3
no
I gt N?
Increase I Move data up Value fib-2 fib-1
yes
Return Value
22
Iterative Fibonacci
N is 8
I is 5
Fib-2 2
Fib-1 3
Value 5
no
I gt N?
Increase I Move data up Value fib-2 fib-1
yes
Return Value
23
Iterative Fibonacci
N is 8
I is 6
Fib-2 3
Fib-1 5
Value 8
no
I gt N?
Increase I Move data up Value fib-2 fib-1
yes
Return Value
24
Iterative Fibonacci
N is 8
I is 7
Fib-2 5
Fib-1 8
Value 13
no
I gt N?
Increase I Move data up Value fib-2 fib-1
yes
Return Value
25
Iterative Fibonacci
N is 8
I is 8
Fib-2 8
Fib-1 13
Value 21
no
I gt N?
Increase I Move data up Value fib-2 fib-1
yes
Return 21
26
fib.scm
27
Code Used
  • fib-iter number -gt number
  • compute the fibonacci number
  • (define (fib-iter n)
  • (let ((nm2 1)
  • (nm1 1))
  • (do ((i 2 ( i 1))
  • (value 1 ( nm2 nm1)))
  • ((gt i n) value)
  • (begin
  • (set! nm2 nm1)
  • (set! nm1 value)))))

28
Questions?
29
Summary
  • You should now know
  • (do )
  • (let )
  • (set! )

30
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com