COSC 3015 Lecture 2 - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

COSC 3015 Lecture 2

Description:

Whenever we have a guess y for the square root of a number x, we can get a ... (the number whose square root we are trying to compute) and a value for the guess. ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 27
Provided by: jeffreyv
Category:
Tags: cosc | lecture | number | of | root | square

less

Transcript and Presenter's Notes

Title: COSC 3015 Lecture 2


1
COSC 3015 Lecture 2
2
Substitution Model of Computation
  • (define (f a)
  • (sum-of-squares ( a 1) ( a 2)))
  • (f 5)
  • (sum-of-squares ( 5 1)( 5 2))
  • ( (square 6)(square 10))
  • ( ( 6 6)( 10 10))
  • ( 36 100)
  • 136

3
The Purpose of the Substitution Model
  • Explains how we should think about procedure
    invocation
  • Not how the interpreter actually works
  • As we proceed, we will develop more sophisticated
    models of the interpreter

4
Normal Order Evaluation
  • An alternative method of evaluation is to only
    perform it when necessary
  • Do substitution until obtain an expression
    involving only primitive operators, then evaluate
  • (f 5)
  • (sum-of-squares ( 5 1) ( 5 2))
  • ( (square( 5 1))(square( 5 2)))
  • ( ( ( 5 1)( 5 1))
  • ( ( 5 2)( 5 2)))

5
  • Note that the evaluation is different
  • Evaluations of ( 5 1) and ( 5 2) are performed
    twice
  • This is as opposed to applicative-order.

6
Evaluating a combination using Normal-Order
Evaluation
  • Evaluate the leftmost subexpression of the
    combination (the operator)
  • If the result is primitive, evaluate the other
    subexpressions and apply the procedure to the
    arguments as usual. If the result is
    nonprimitive, apply to unevaluated operand
    expressions

7
Procedure Evaluation in Normal-Order Evaluation
  • The substitution rule is the same, except now the
    unevaluated expressions are substituted for the
    formal parameters in the body of the procedure.
  • (sum-of-squares ( 5 1) ( 5 2))
  • ( (square( 5 1))(square( 5 2)))
  • ( ( ( 5 1)( 5 1))
  • ( ( 5 2)( 5 2)))

8
Predicates
  • Predicates are procedures that return true or
    false.
  • Scheme represents these constants as t and f
    respectively
  • The absolute-value procedure abs makes use of the
    primitive predicates gt, lt, and .
  • These take two numbers as arguments and test
    whether the first number is, respectively,
    greater than, less than, or equal to the second
    number, returning true or false accordingly.
  • (lt 5 6)
  • t

9
Predicate connectives
  • For combining predicates
  • (and lte1gtltengt) special form
  • (or lte1gtltengt)- special form
  • (not lte1gt)
  • Where each of the ei are predicates
  • These have the behavior that we are used to
  • E.g. and evaluates its arguments left to right
    and returns the first one that is true, or
    returns false

10
What if predicate connectives were normal
procedures?
  • (and (lt 5 6) (gt (4) 8))

11
Conditional Expressions and Predicates
  • (cond (ltp1gt lte1gt)
  • (ltp2gt lte2gt)
  • (ltpngt ltengt))
  • Each clause contains a predicate and an
    expression
  • First true predicate has its expression evaluated
  • Special use of else

12
Examples
  • (define (abs x)
  • (cond ((gt x 0) x)
  • (( x 0) 0)
  • ((lt x 0) (- x))))
  • (define (abs x)
  • (cond ((gt x 0) x)
  • (else (- x)))
  • (define (abs x)
  • (if (gt x 0) x (- x)))

13
Example Problem 1
  • Write a procedure sign that returns 1 if its
    argument is positive, -1 if its argument is
    negative, and 0 if its argument is 0.

14
Example Problem 1
  • Write a procedure sign that returns 1 is its
    argument is positive, -1 if its argument is
    negative, and 0 if its argument is 0.
  • (define (sign x)
  • (cond ((lt x 0) -1)
  • ((gt x 0) 1)
  • (else 0)))

15
Example Problem 2
  • Define a procedure called true-false that takes
    one argument and returns 1 if the argument is
    true and 0 if it is false.

16
Example Problem 2
  • Define a procedure called true-false that takes
    one argument and return 1 if the argument is true
    and 0 if it is false.
  • (define (true-false x)
  • (if x 1 0))
  • (define (true-false x)
  • (cond (x 1)
  • (else 0)))

17
Example Square Root by Newtons Method
  • Whenever we have a guess y for the square root of
    a number x, we can get a better guess by
    averaging x and x/y
  • Example (compute the sqrt of 2)
  •   1 (2/1) 2 ((2 1)/2) 1.5  
  •  1.5 (2/1.5) 1.3333 ((1.3333 1.5)/2)
    1.4167   
  • 1.4167 (2/1.4167) 1.4118 ((1.4167
    1.4118)/2) 1.4142   
  • 1.4142 ......

18
Strategy for Newtons Method
  • We start with a value for the radicand (the
    number whose square root we are trying to
    compute) and a value for the guess.
  • If the guess is good enough for our purposes, we
    are done if not, we must repeat the process
    (i.e. compute average of x and x/y) with an
    improved guess.

19
Code for Newtons Method
  • (define (sqrt-iter guess x)  (if (good-enough? gu
    ess x)      guess      (sqrt-iter (improve guess
     x)                 x)))
  • (define (improve guess x)  (average guess (/ x gu
    ess)))
  • (define (average x y)  (/ ( x y) 2))
  • (define (good-enough? guess x)  (lt (abs (- (squar
    e guess) x)) 0.001))

20
Top-Level Procedure for Newtons Method
  • (define (sqrt x)  (sqrt-iter 1.0 x))

21
Points to Note
  • Sqrt-iter does recursively what you are used
    doing iteratively
  • The problem breaks up naturally into subproblems
  • Each subproblem is handled by a different
    procedure
  • Decomposition is not just dividing a problem into
    pieces
  • Its doing so in a way that exposes the structure

22
Points contd
  • Procedures give us a mechanism for abstraction
  • i.e., they let us suppress detail
  • It doesnt matter how the square procedure
    computes the square

23
Name Isolation
  • Name isolation is an important method of
    abstraction
  • Which allows us to suppress details
  • First type formal parameters are local to the
    body of a procedure (weve seen this)
  • Second type we would like some procedures to be
    local to the body of other procedures

24
Newtons Method with Local Naming
  • (define (sqrt x)  (define (good-enough? guess x)
        (lt (abs (-(square guess) x)) 
  • 0.001))  (define (improve guess x)    (averag
    e guess (/ x guess)))  (define (sqrt-iter guess x
    )    (if (good-enough? guess x)        guess   
         (sqrt-iter
  • (improve guess x) x)))  (sqrt-iter 1.0 x))

25
A Simplified Solution
  • (define (sqrt x)  (define (good-enough? guess)  
      (lt (abs (-
  • (square guess) x)) 0.001))  (define (improv
    e guess)    (average guess (/ x guess)))  (defin
    e (sqrt-iter guess)    (if (good-enough? guess) 
           guess        (sqrt-iter (improve guess))))
      (sqrt-iter 1.0))

26
Block Structure
  • A good solution to this simplest type of
    name-packaging
  • First introduced in Algol 60
  • Better solutions are coming
Write a Comment
User Comments (0)
About PowerShow.com