Assignment:Changing Data
CMSC 11500
Introduction to Computer Programming
November 1, 2002
Roadmap
• Recap: Binary Search Trees
• State: Changing Data over Time– Assignment with set!– Multiple statements with begin– Objects with state
• Traffic light example• Bank account example
• Summary
Sets: Binary Search Trees• Bst is:
• 1) #f• 2) (make-bt val left right)
– , where val: number; left, right: bst
• (define-struct bt (val left right))• INVARIANT: For node n, all vals in (bt-left n) < n; all vals in
(bt-right n) > n
• Define operations on sets with bst’s– Element-of?, Adjoin, Intersection, Union,..– More efficient – O(log n) – search, adjunction..– Functions exploit & maintain invariant
• If true of inputs, must be kept true of output
State of the Data
• Computationally model objects in world
• Objects change over time– E.g. traffic light: red, yellow, green– Bank account: balance up/down– Age, temperature, etc,…
Changing State in Scheme
• So far, value in variable once– Bind values to variables with formal params
• To change state must change value assigned to variable
• Assignment: set!– (set! var exp)
• var: variable name; exp: any scheme expression
– (set! x 1)– (set! x (factorial 10))
Evaluating set!
• Expressions evaluate to values– (+ 3 4) => 7– (set! x 1) ??? Void - nothing
• Set! is produces an effect, not a value– Changes value of variable
• Problem: Need to set! AND return value• Solution: Do both!
– (begin exp1 …. expn)– Does evaluates all exps; returns value of last– (begin (set! x (+ 3 4)) x) -> 7
Example: Traffic Lights
• State of traffic light: color– TL-color is ‘green, ‘yellow, or ‘red– ; contract: current-color: TL-color
• (define current-color ‘red)– ? (set! current-color 5)– ? (set! current-color ‘green)
Changing Traffic Lights
• First step: What would it change to?
• Function: – ;;contract:next-color: TL-color -> TL-color– ;; purpose: To identify next color in sequence
(define (next-color color) (cond ((eq? color ‘red) ‘green)
((eq? color ‘yellow) ‘red) ((eq? color ‘green) ‘yellow)))
Really Changing Lights
• Key: change value of state variable with set!
(define (turn-light) (begin (set! current-color (next-color current-color)) current-color))) (turn-light)
Return value?Current-color?
Example: Bank Account
• State of bank account: balance– Varies over time: withdrawal, deposit, check..
• (define balance 100)
• (set! balance (- balance 25))
• Goal: encapsulation in function– Test if enough money
• If there is, reduce balance• O.w., report error
Make-Withdraw
• Contract: – ;;make-withdraw: number ->
(number -> number)– Note: returns a procedure
• Purpose:– ;; to create a procedure to process withdrawal
Make-withdraw
(define (make-withdraw initial-balance) (let ((balance initial-balance)) (lambda (amount)
(if (<= amount balance) (begin
(set! balance (- balance amount))balance)
(error “Insufficient funds!!))))
Using Make-withdraw
• (define w1 (make-withdraw 100))
• (w1 20)– > 80
• (w1 100)– > “Insufficient funds!!”
• (w1 60)– > 20
Summary
• State variables– Represent objects: state changes over time
• Forms:– (set! var expression)– (begin exp1 … expn)
• Modeling traffic lights and bank accounts