; Jay adds an assignment statement. Is this horrible or useful? ; We've so far used functional programs, to express ourselves. ; Functional programs encode mathematical truths. ; Processes evolved by such programs ; can be understood by substitution. ; Methods may be distinguished ; by the choice of truths expressed. ; Functional programs are always the same unique mapping from ; arguments to return values. UNTIL ASSIGNMENT COMES INTO PLAY. ; (set! ) (DEFINE COUNT 1) (DEFINE (DEMO X) (SET! COUNT (1+ COUNT)) (+ X COUNT)) ; (DEMO 3) ; => 5 ; ; (DEMO 3) ; => 6 ; ; (let ((var1 e1) (var2 e2)) ; e3) ; IS THE SAME AS ; ((lambda (var1 var2) e3) e1 e2) ; 5A:(9m37s) DEMO is not a mathematical function! ; Excellent QUESTIONS (about LET, etc.) 5A:(17m) BREAK ; Enhancing the SUBSTITUTION MODEL 1B:(5m5s) ; with the ENVIRONMENT MODEL 5A:(22m57s). ; Jay describes what bound and free variables are. ; 5A:(32m37s) Then he describes the ENVIRONMENT MODEL a little more ; and introduces frames, sharing of environment and shadowing ; of variables. ; 5A:(37m) Jay describes what a procedure is. And then he ; introduces the two rules of the new model. ;;; ENVIRONMENT MODEL ; Rule 1: A procedure object is applied to a set of ; arguments by constructing a frame, binding the ; formal parameters of the procedure to the actual ; arguments of the call, and then evaluating the body ; of the procedure in the context of the new ; environment constructed. The new frame has as its ; enclosing environment the environment part of the ; procedure object being applied. ; ; 5A:(41m38s) "How do we get this frame?" Giving reason for rule two. ; CONSTRUCTION OF PROCEDURES ; Rule 2: A lambda-expression is evaluated relative ; to a given environment as follows: a new procedure ; object is formed. Combining the text (code) of the ; lambda-expression with a pointer to the environment ; of evaluation. ; interesting QUESTIONS 5A:(44m45s) BREAK ; Jay must come with a good excuse, as to why he did this horrible thing... ; Now we all have lost the ability to define precious mathematical truths, ; because a procedure could internally use assignment so that successive ; calls do different things. (DEFINE MAKE-COUNTER (LAMBDA (N) (LAMBDA () (SET! N (1+ N)) N))) (DEFINE C1 (MAKE-COUNTER 0)) (DEFINE C2 (MAKE-COUNTER 10)) ; 5A:(53m46s) These two are placed in two different sub-environments. ; So MAKE-COUNTER really is just a constructor for environments. (C1) ;=> 1 (C2) ;=> 11 (C1) ;=> 2 (C2) ;=> 12 ; 5A:(56m44s) What is an object? ; 5A:(1h36s) "And so I think by introducing assignment and objects, ; we have opened ourselves up to all the horrible questions ; of philosophy..." --Jay ; 5A:(1h1m45s) ;;; ACTIONS AND IDENTITY ; We say that an action A, had an effect on an ; object X, (or equivalently, that I was changed by ; A) if some property P, which was true of X before ; A became false of X after A. ; ; We say that two objects, X and Y, are the same if ; any action which has an effect on X has the same ; effect on Y. ; Jay emphasizes that assignment should not be used where it is not ; needed. So to say, if something can be expressed mathematically, ; you should do so. ;;; Cesaro's method for estimating Pi: ;;; Prob(gcd(n1,n2)=1) = 6/(Pi*Pi) ; ; (define (estimate-pi n) ; (sqrt (/ 6 (monte-carlo n cesaro)))) ; (define (cesaro) ; (= (gcd (rand) (rand)) 1)) ; (define (monte-carlo trials experiment) ; (define (iter remaining passed) ; (cond ((= remaining 0) ; (/ passed trials)) ; ((experiment) ; (iter (-1+ remaining) ; (1+ passed))) ; (else ; (iter (-1+ remaining) ; passed)))) ; (iter trials 0)) ; (define rand ; (let ((x random-init)) ; (lambda () ; (set! x (rand-update x)) ; x))) ; ; Doing the same using functions would be a mess, ; as shown later in the lecture. ; (END)