Foundations of Programming Paradigms 2000

 10  Modularity, Objects and State


What's in This Set of Notes ?


10.1 Introduction


10.2 Assignment and Local State


Special Forms

(set! <name> <new-value>)
(begin <exp1> <exp2>  ... <expn>)


Problem: balance is defined in the global environment

(define new-withdraw
    (let (( balance 100))
       (lambda (amount)
            (if (>= balance amount)
                (begin
                    (set! balance (- balance amount))
                    balance)
                    "Insufficient funds"))))

 
  • Let establishes local environment
  • Causes problems for our substitution model (see later)
  • Maybe we can try to write it again

  •  
    (define (make-withdraw balance)
           (lambda (amount)
                (if (>= balance amount)
                    (begin
                        (set! balance (- balance amount))
                        balance)
                        "Insufficient funds")))
    (define W1 (make-withdraw 100))
    (define W2 (make-withdraw 100))
    (W1 50) => 50
    (W2 70) => 30
    (W2 40) => "Insufficient funds"
    (W1 40) => 10


    BankAccount Object

    (define (make-account balance)
        (define (withdraw amount)
             (if (>= balance amount)
                    (begin
                        (set! balance (- balance amount))
                        balance)
                        "Insufficient funds"))
        (define (deposit amount)
            (set! balance (+ balance amount))
            balance)
        (define (dispatch method)
            (cond     ((eq? method 'withdraw) withdraw)
                          ((eq? method 'deposit) deposit)
                          (else (error "Unknown Request -- MAKE ACCOUNT" method))))
        dispatch)
    (define account (make-account 100))
     
    ((account 'withdraw) 60) => 40
    ((account 'withdraw) 60) =>  "Insufficient funds"
    ((account 'deposit) 50) =>  90
    ((account 'clear)) =>  "Unknown Request -- MAKE ACCOUNT" clear


    Benefits of Introducing Assignment

  • Allows us to view a system as collection of objects with local state is a powerful technique for maintaining a modular design
  • Allows us to "Remember" things
  • Entities don't "leak-out" into other parts of program
  • Allows encapsulation (hidden time varying local state)

  • Cost of Introducing Assignment

    (define W (make-simplified-withdraw 25))
    (W 20) => 5
    (W 10) => -5
  • With one that does not use it
  • Applying Substitution Model to make-decrementer
  • Applying Substitution Model to make-simplified-withdraw
  • Now we apply the operator by substituting 20 for amount in the body of the lambda expression

  • (set! balance (-25 20)) 25
    (set! balance 5 ) 25

     
  • If we adhere to the substitution model, we would have to say that the meaning of the procedure is to first set balance to 5 and then return 25.
  • We would have to distinguish between to values for balance which substitution model can't handle
  • Substitution models assumes symbols are names for values in environment and that they don't change

  • Sameness and Change

    (define w1 (make-simplified-withdraw 2))
    (define w2 (make-simplified-withdraw 25))
    (w1 20) => 5
    (w1 20) => -15
    (w2 20) => 5


    Cost of Assignment

    Example of Imperative Style

    (define (factorial n)
        (let ((product 1)
                (counter 1))
            (define (iterate)
                (if (> counter n)
                        product
                        (begin
                                (set! product (* counter product))
                                    // note that the order of these set! statements is important
                                (set! counter (+ counter 1))
                                (iterate))))
            (iterate)))

    Example of Functional Style

    (define (factorial n)
        (define (iterate product counter)
            (if (> counter n)
                product
                (iterate (* counter product) (+ counter 1))))
        (iterate 1 1))



    10.3 Streams


    Compute sum of primes in an interval accumulator  style

    (define (sum-primes a b)
     (define (iteration count accumulator)
      (cond ((> count b) accumulator)
       ((prime? count) (iteration (+ count 1) (+ count accumulator)))
       (else (iteration (+ count 1) accumulator))))
     (iteration a 0))

     


    Compute sum of primes in an interval List style

    (define (sum-primes a b)
     (accumulate + 0 (filter-prime? (enumerator-iterval a b))))

     

     
     
     

    What about (car (cdr (filter-prime? (enumerate-interval 10000 1000000))))?

    This expression finds second prime but at what cost?


    10.4 Stream Approach

    Empty Stream

    Building and accessing parts of a Stream

    Find element n of stream

    Apply procedure to values in stream


    10.5 Stream Implementation


    Implementation in action


    10.6 Infinite Streams

    Using delay/force to build infinite streams


    Let's build infinite stream with integers not divisible by 7


    Let's build an infinite stream of Fibonacci numbers


    Formulating iterations as stream processes – revisit Newton's method


    Using Streams with objects