Global state: - PowerPoint PPT Presentation

1 / 3
About This Presentation
Title:

Global state:

Description:

... amt)) (print 'Your check just bounced!')))) ((equal? operation 'balance) ... (my-acct 'balance) 225 == ((my-acct 'withdraw) 226) Your check just bounced! ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 4
Provided by: dexter8
Category:
Tags: bounced | global | state

less

Transcript and Presenter's Notes

Title: Global state:


1
Global state (define var 0) (define global
(lambda (amt)    (set! var ( var amt))
var)) No state (define no (lambda (amt)
   (let ((var 0))      (set! var ( var amt))
     var))) Local state (define local  (let
((var 0))    (lambda (amt)      (set! var (
var amt))      var)))
2
((define make-account   (lambda (balance)    
(lambda (operation)       (cond
((equal? operation 'deposit)     (lambda
(amt)       (if (gt amt 0)   
(set! balance ( balance amt))   
(print "Can't deposit a negative amount!"))))
    ((equal? operation 'withdraw)     
(lambda (amt)       (if (and (gt amt 0) (gt
balance amt))    (set! balance (-
balance amt))    (print "Your check
just bounced!"))))     ((equal? operation
'balance)      balance)     (else
(error "Unknown operation"))))))
3
gt (define my-acct (make-account 100))gt
(my-acct 'balance)100gt ((my-acct 'deposit)
150)gt (my-acct 'balance)250gt ((my-acct
'withdraw) 25)gt (my-acct 'balance)225gt
((my-acct 'withdraw) 226)Your check just
bounced!gt
Write a Comment
User Comments (0)
About PowerShow.com