Imperative Programming Back to scheme Scheme is a
Imperative Programming
Back to scheme • Scheme is a functional language • In some cases there is a need to capture objects state • E. g. bank account
Imperative Programming Imperative programming is characterized by: • 1. Understanding variables as storage places (addresses). • 2. Assignment – an instruction for changing the content of a variable. • 3. Operational semantics – a computation is a sequence of system-states.
Scheme has constructs for Imperative Programming!
(define withdraw (let ((balance 100)) (lambda (amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds"))))
Substitution model? (define withdraw (let ((balance 100)) Substitution model can’t (lambda (amount) explain assignment! (if (>= 100 amount) (begin (set! 100 (- 100 amount)) balance) "Insufficient funds"))))
(define make-withdraw (lambda (balance) (lambda (amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds"))))
(define make-withdraw (lambda (balance) (lambda (amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds"))))
(define make-account (lambda (balance) (letrec ((withdraw (lambda (amount) …. (deposit (lambda (amount) (set! balance (+ balance amount)) balance)) (dispatch (lambda (m) (cond ((eq? m ’withdraw) ((eq? m ’deposit) (else (error "Unknown request-make-account" m)))) dispatch )
(define transact (lambda (account transaction-type amount) ((account transaction-type) amount)))
env-model diagram
Sameness and change • • (define W 1 (make-simplified-withdraw 100)) (define W 2 (make-simplified-withdraw 100)) > (W 1 30) 70 > (W 1 70) 0 > (W 2 70) 30
Functional (define factorial (lambda (n) (letrec ((iter (lambda(product counter) (if (> counter n) product (iter (* counter product) (+ counter 1)))) )) (iter 1 1))))
Imperative (define factorial (lambda (n) (let ((product 1) (counter 1)) (letrec ((iter (lambda() (if (> counter n) product (begin (set! product (* counter product)) (set! counter (+ counter 1)) (iter)))))
Function calls • In functional languages , usually by value • In Imperative languages can be by reference
Simulating Object Oriented Languages • In fact, the account object we have created is very similar to a class • We can go further and separate method selection from application (define get-method (lambda (obj m))) (define withdraw-method (get-method acc 1 ’withdraw)) > (withdraw-method 30)
Method application (define send (lambda (obj message args) (apply (get-method obj message) args))) > (send acc 1 ’withdraw ’(35))
Static properties (define make-interest-account (let ((interest 1)) (lambda (balance) (letrec ((withdraw (lambda (amount) … • dispatch))))
"Inheritence” (define make-limited-account (lambda (limit acct) (letrec ((withdraw (lambda (amount) (if (> amount limit) ’over-limit ((acct ’withdraw) amount)))) (dispatch (lambda (message) (if (eq? message ’withdraw) withdraw (acct message))))) dispatch)))
Delegation (define make-passwd-account (lambda (password acct) (letrec ((change-password (lambda (new-pass) (set! password new-pass))) (dispatch (lambda (pass message) (if (eq? password) (if (eq? message ’change-password) change-password (acct message))
letrec with set! (letrec ((f 1 lambda-exp 1). . . (fn lambda-expn)) e 1. . . em) => (let ((f 1 ’unassigned). . . (fn ’unassigned)) (set! f 1 lambda-exp 1). . . (set! fn lambda-expn) e 1. . . em)
Boxed type • In Dr. Racket, a mutable composite type must wrap its mutable components with the box type. Dr. Racket box type: A box is a minimal mutable storage. (box v) Returns a new mutable box that contains v. (unbox box) Returns the content of box. For any v, (unbox (box v)) returns v. • (set-box! box v) • Sets the content of box to v. • •
box • • • (define y 3) (set! y (cons (box 1) (box 2))) >y ’(#&1. #&2) ; the printed form of a box includes the prefix "#&" ; before the box content. > (unbox (car y)) 1 > (set-box! (car y) 3) >y ’(#&3. #&2) > (unbox (car y)) 3
Type checking with set! • • Typing rule set! : For every: type assignment TA, expression e, and type expression S: If TA |- e: S, TA |- x: S, Then TA |- (set! x e): unit
- Slides: 25