Com S 342 meeting -*- Outline -*- * data structure mutation (4.5.3) ------------------------------------------ DATA STRUCTURE MUTATION lists and pairs: set-car! : (-> ((pair S T) S) void) set-cdr! : (-> ((pair S T) T) void) vectors: vector-set! : (-> ((vector T) number T) void) def: an object is *mutable* if an object is *immutable* if ------------------------------------------ Q: What is vector-set! like in C, C++, Pascal? Q: Do you think Scheme also allows mutation of strings? integers? ... def: an object is *mutable* if its value (state) can be changed (over time). ... an object is *immutable* if it can't be changed. draw a graph time vs. value for immutable and mutable objects ** what is an object? an object is a box (memory cell) containing some value or (references to) other objects don't have to explicitly think about the pointers, since implicit and everywhere can't manipluate them just speak of on object containing another and expression denoting an object ------------------------------------------ EXPRESSIONS EVALUATE TO OBJECTS ___________ (vector 2 3 4) ==> |_*_|_*_|_*_| | | | _v_ _v_ _v_ |2| |3| |4| ____ 'hi ==> |_hi_| ___ (+ 2 1) ==> |_3_| ------------------------------------------ we suppress the pictures of evaluation of 2, 1, 3, 4... ** what does cons do? cons allocates a new "cons cell" (two cells) and puts (references to) its two arguments in the car and cdr ------------------------------------------ WHAT CONS DOES _____ _____ ____ (cons 3 '()) ----> | | --|--> |_()_| |__|__|_____| | | _v_ |_3_| _____ _____ ____ (cons 3 '()) ----> | | --|--> |_()_| |__|__|_____| | | _v_ |_3_| _____ _____ ___ (cons 3 4) ------> | | --|--> |_4_| |__|__|_____| | | _v_ |_3_| ------------------------------------------ ------------------------------------------ ANOTHER EXAMPLE (cons 'x (cons 'y (cons 'z '()))) ------------------------------------------ ** set-car! and set-cdr! ------------------------------------------ EFFECT OF SET-CAR! AND SET-CDR! (define a (cons 1 (cons 2 '()))) (set-car! a 342) (set-cdr! a 227) ------------------------------------------ ** modeling objects in Scheme ------------------------------------------ AN ADT FOR OBJECTS (CELLS) Operations make-cell: (-> (datum) cell) cell?: (-> (datum) boolean) cell-ref: (-> (cell) datum) cell-set!: (-> (cell datum) void) cell-swap!: (-> (cell cell) void) Behavior ------------------------------------------ ... (cell? (make-cell x)) = #t (cell? (vector 3 4)) = #f (cell-ref (make-cell x)) = x (define a-cell (make-cell 'a)) (define cell2 (make-cell 2)) (begin (cell-set! a-cell 'b) (cell-ref a-cell)) = 'b (begin (cell-swap! a-cell cell2) (list 'a-cell (cell-ref a-cell) 'cell2 (cell-ref cell2))) = '(a-cell 2 cell2 b) work out, or refer them to, Figure 4.5.1, page 125 use a vector representation ------------------------------------------ CELL IMPLEMENTATION (Fig. 4.5.1) (define cell-tag "cell") (define make-cell ;; TYPE: (-> (Expressed-Value) (cell T)) (lambda (x) (define cell? ;; TYPE: (-> (datum) boolean) (lambda (x) (and (vector? x) (= (vector-length x) 2) (eq? (vector-ref x 0) cell-tag)))) (define cell-ref ;; TYPE: (-> ((cell T)) T) (lambda (x) (define cell-set! ;; TYPE: (-> ((cell T) T) void) (lambda (x value) (define cell-swap! ;; TYPE: (-> ((cell T) (cell T)) void) (lambda (cell-1 cell-2) (let ((temp (cell-ref cell-1))) (cell-set! cell-1 (cell-ref cell-2)) (cell-set! cell-2 temp)))) ------------------------------------------ ... (vector cell-tag x))) ... (vector-ref x 1) ... (vector-set! x 1 value)