(1) (2) (3) Example projector Announcements (4) (5) (6) Example, 2 projector (7) (8) (9) projector Note: Write Announcements and Example before class starts! SLIDE 1 0. ON BOARD 1: (define make-adder (lambda (x) (lambda (y) (+ x y)))) ON BOARD 4: (define add-2 (make-adder 2)) (define add-3 (make-adder 3)) (add-2 5) ; ==> 7 (add-3 5) ; ==> 8 (add-2 12); ==> 14 (define make-counter (lambda (balance) (lambda (amt) (set! balance (+ balance amt)) balance))) (define count-2 (make-counter 2)) (define count-3 (make-counter 3)) (count-2 5) ; ==> {7 later} (count-3 5) ; ==> {8 later} (count-2 12); ==> {19 later} ON BOARD 3: Quiz, Tomorrow, 5-7pm xor 7-9pm J++ soon; start learning your favorite Java environment Models, State, and Side-Effects 1. Introduction, 5 mins., 10:05 - 10:10 Building models of the real world requires modularity that reflects the systems being modeled. State: hidden, visible - state is the information needed to make the system repeatable Functions: all state is visible "what you put in determines what you get out" The real world seems to have hidden state, or at least we like to see it that way! PROP? 2. Today's Example, 5 mins., 10:10 - 10:15 Since things appear to change in the real world, we add a new special form, set!, to our language to model these changes. SET! takes a variable and an expression and changes the value of the variable. This is the first truly controversial change we are making to Scheme, and today we'll investigate the pros, cons (bad pun?), and implementation issues. ADD TO BOARD 4 (count-2 5) ; ==> 7 (count-3 5) ; ==> 8 (count-2 12) ; ==> 19 (count-2 5) ; ==> 24 Unlike make-adder, the procedures created by make-counter appear to remember - they have hidden state. 3. The Good, 5 mins., 10:15 - 10:20 We can write programs with a modularity that matches our intuitions about the real world. The example from the book of a random number generator is excellent. Functional approach: just a pure function rand-update, that has an explicit state needed to compute the next value. 4. The Bad, 5 mins., 10:20 - 10:25 5. The Ugly, 10 mins., 10:25 - 10:40 6. Pairs revisited, 10 mins., 10:40 - 10:50 7. Review, 5 minutes, 10:50 - 10:55