Imperative Programming. Back to scheme Scheme is a functional language In some cases there is a need...
-
Upload
prosper-weaver -
Category
Documents
-
view
217 -
download
1
Transcript of Imperative Programming. Back to scheme Scheme is a functional language In some cases there is a need...
![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.](https://reader036.fdocuments.net/reader036/viewer/2022062714/56649d0f5503460f949e5649/html5/thumbnails/1.jpg)
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.](https://reader036.fdocuments.net/reader036/viewer/2022062714/56649d0f5503460f949e5649/html5/thumbnails/2.jpg)
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.](https://reader036.fdocuments.net/reader036/viewer/2022062714/56649d0f5503460f949e5649/html5/thumbnails/3.jpg)
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.](https://reader036.fdocuments.net/reader036/viewer/2022062714/56649d0f5503460f949e5649/html5/thumbnails/4.jpg)
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.](https://reader036.fdocuments.net/reader036/viewer/2022062714/56649d0f5503460f949e5649/html5/thumbnails/5.jpg)
(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.](https://reader036.fdocuments.net/reader036/viewer/2022062714/56649d0f5503460f949e5649/html5/thumbnails/6.jpg)
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.](https://reader036.fdocuments.net/reader036/viewer/2022062714/56649d0f5503460f949e5649/html5/thumbnails/7.jpg)
(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.](https://reader036.fdocuments.net/reader036/viewer/2022062714/56649d0f5503460f949e5649/html5/thumbnails/8.jpg)
(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.](https://reader036.fdocuments.net/reader036/viewer/2022062714/56649d0f5503460f949e5649/html5/thumbnails/9.jpg)
(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.](https://reader036.fdocuments.net/reader036/viewer/2022062714/56649d0f5503460f949e5649/html5/thumbnails/10.jpg)
(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.](https://reader036.fdocuments.net/reader036/viewer/2022062714/56649d0f5503460f949e5649/html5/thumbnails/11.jpg)
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.](https://reader036.fdocuments.net/reader036/viewer/2022062714/56649d0f5503460f949e5649/html5/thumbnails/12.jpg)
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.](https://reader036.fdocuments.net/reader036/viewer/2022062714/56649d0f5503460f949e5649/html5/thumbnails/13.jpg)
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.](https://reader036.fdocuments.net/reader036/viewer/2022062714/56649d0f5503460f949e5649/html5/thumbnails/14.jpg)
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.](https://reader036.fdocuments.net/reader036/viewer/2022062714/56649d0f5503460f949e5649/html5/thumbnails/15.jpg)
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.](https://reader036.fdocuments.net/reader036/viewer/2022062714/56649d0f5503460f949e5649/html5/thumbnails/16.jpg)
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.](https://reader036.fdocuments.net/reader036/viewer/2022062714/56649d0f5503460f949e5649/html5/thumbnails/17.jpg)
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.](https://reader036.fdocuments.net/reader036/viewer/2022062714/56649d0f5503460f949e5649/html5/thumbnails/18.jpg)
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.](https://reader036.fdocuments.net/reader036/viewer/2022062714/56649d0f5503460f949e5649/html5/thumbnails/19.jpg)
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.](https://reader036.fdocuments.net/reader036/viewer/2022062714/56649d0f5503460f949e5649/html5/thumbnails/20.jpg)
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.](https://reader036.fdocuments.net/reader036/viewer/2022062714/56649d0f5503460f949e5649/html5/thumbnails/21.jpg)
![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.](https://reader036.fdocuments.net/reader036/viewer/2022062714/56649d0f5503460f949e5649/html5/thumbnails/22.jpg)
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.](https://reader036.fdocuments.net/reader036/viewer/2022062714/56649d0f5503460f949e5649/html5/thumbnails/23.jpg)
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.](https://reader036.fdocuments.net/reader036/viewer/2022062714/56649d0f5503460f949e5649/html5/thumbnails/24.jpg)
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.](https://reader036.fdocuments.net/reader036/viewer/2022062714/56649d0f5503460f949e5649/html5/thumbnails/25.jpg)
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