25
Imperative Programming

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

Embed Size (px)

Citation preview

Page 1: 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

Page 2: 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

Back to scheme

• Scheme is a functional language

• In some cases there is a need to capture objects state

• E.g. bank account

Page 3: 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.

Page 4: 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

Scheme has constructs for Imperative Programming!

Page 5: 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

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

Page 6: 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

Substitution model?

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

Substitution model can’t

explain assignment!

Page 7: 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

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

Page 8: 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

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

Page 9: 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

(define make-account (lambda (balance) (letrec ((withdraw (lambda (amount) …. (deposit (lambda (amount) (set! balance (+ balance amount)) balance)) (dispatch (lambda (m) (cond ((eq? m ’withdraw) withdraw) ((eq? m ’deposit) deposit) (else (error "Unknown request-make-account" m))))dispatch)

Page 10: 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

(define transact (lambda (account transaction-type amount) ((account transaction-type) amount)))

Page 11: 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

env-model diagram

Page 12: 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

Sameness and change

• (define W1 (make-simplified-withdraw 100))• (define W2 (make-simplified-withdraw 100))• > (W1 30)• 70• > (W1 70)• 0• > (W2 70)• 30

Page 13: 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

Functional

(define factorial (lambda (n) (letrec ((iter (lambda(product counter) (if (> counter n) product (iter (* counter product) (+ counter 1)))) )) (iter 1 1))))

Page 14: 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(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))))(iter)))))

Page 15: 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

Function calls

• In functional languages , usually by value

• In Imperative languages can be by reference

Page 16: 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

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) (obj m)))(define withdraw-method (get-method acc1

’withdraw))> (withdraw-method 30)

Page 17: 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

Method application

(define send (lambda (obj message args) (apply (get-method obj message) args)))> (send acc1 ’withdraw ’(35))

Page 18: 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

Static properties

(define make-interest-account (let ((interest 1)) (lambda (balance) (letrec ((withdraw (lambda (amount) …•dispatch))))

Page 19: 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

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)))

Page 20: 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

Delegation(define make-passwd-account (lambda (password acct) (letrec ((change-password (lambda (new-pass) (set! password new-pass))) (dispatch (lambda (pass message) (if (eq? pass password) (if (eq? message ’change-password) change-password (acct message))

Page 21: 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
Page 22: 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

letrec with set!

(letrec ((f1 lambda-exp1) ...(fn lambda-expn)) e1 ... em) =>

(let ((f1 ’unassigned) ... (fn ’unassigned)) (set! f1 lambda-exp1) ... (set! fn lambda-expn) e1 ... em)

Page 23: 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

Boxed type

• In DrRacket, a mutable composite type must wrap its mutable components with the box type.

• DrRacket 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.

Page 24: 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

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

Page 25: 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

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