CS302: Paradigms of Programming Imperative Programming
Manas Thakur
Feb-June 2020
CS302: Paradigms of Programming Imperative Programming Manas Thakur - - PowerPoint PPT Presentation
CS302: Paradigms of Programming Imperative Programming Manas Thakur Feb-June 2020 Credits Some images have been taken from the texinfo format of Structure and Interpretation of Computer Programs, Second Edition,
Feb-June 2020
2
3
4
5
6
7
8
9
10
11
(define W1 (make-withdraw 100)) (define W2 (make-withdraw 100)) > (W1 50) > 50 > (W2 70) > 30 > (W2 40) > “Insufficient funds” > (W1 40) > 10
12
13
14
Imperative Functional
15
((make-decrementer 25) 20) ==> ((lambda (amount) (- 25 amount)) 20) ==> (- 25 20) ==> 5 ((make-simplified-withdraw 25) 20) ==> ((lambda (amount) (set! balance (- 25 amount) 25) 20) ==> (set! balance (- 25 20)) 25 ==> Set balance to 5 and return 25
16
(define D1 (make-decrementer 25)) (define D2 (make-decrementer 25)) (define W1 (make-withdraw 25)) (define W2 (make-withdraw 25))
Are D1 and D2 the same? > Arguably yes, because each could be substituted for the other. Are W1 and W2 the same? > They look to be, but aren’t: (W1 20) 5 (W1 20)
(W2 20) 5
17
18
19