Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Imperative Programming

2 Back to scheme Scheme is a functional language In some cases there is a need to capture objects state E.g. bank account

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

4 Scheme has constructs for Imperative Programming!

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

6 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!

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

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

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

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

11 env-model diagram

12 Sameness and change (define W1 (make-simplified-withdraw 100)) (define W2 (make-simplified-withdraw 100)) > (W1 30) 70 > (W1 70) 0 > (W2 70) 30

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

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

15 Function calls In functional languages, usually by value In Imperative languages can be by reference

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

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

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

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

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

21

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

23 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.

24 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

25 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


Download ppt "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."

Similar presentations


Ads by Google