topic 19
play

Topic 19 (define x (cons 'a 'b)) Mutable Data Objects x --> (a - PDF document

Mutable data Basic operations like cons, car, cdr can construct list structure and select its parts, but cannot modify. Topic 19 (define x (cons 'a 'b)) Mutable Data Objects x --> (a . b) Primitive mutators for pairs: set-car! And


  1. Mutable data Basic operations like cons, car, cdr can construct list structure and select its parts, but cannot modify. Topic 19 (define x (cons 'a 'b)) Mutable Data Objects x --> (a . b) Primitive mutators for pairs: set-car! And set-cdr! CHANGE the list structure itself. Section 3.3 (set-car! x 1) x --> (1 . b) (set-cdr! x 2) x --> (1 . 2) Fall 2008 Programming Development 1 Fall 2008 Programming Development 2 Techniques Techniques Mutators Some comparisons (define x '((a) b)) • Procedures set-car! and set-cdr! are (define y '(c d)) examples of mutators (define z (cons y (cdr x))) • Mutators are procedures that change the z --> ((c d) b) contents of data structures x --> ((a) b) (set-car! x y) x --> ((c d) b) (set-cdr! x y) x --> ((c d) c d) (set-car! (cdr x) '(a)) x --> (((a) d) (a) d) Fall 2008 Programming Development 3 Fall 2008 Programming Development 4 Techniques Techniques Circular lists Mutation of shared lists (define x '(2)) (define x '(a b c)) (define y (cons 1 x)) (define y (cons x x)) (set-cdr! x y) (define z (cons x '(a b c))) y --> (1 2 1 2 1 2 ... y --> ((a b c) a b c) z --> ((a b c) a b c) [DrScheme is smart enough not to print the whole list] (set-cdr! (cdr x) ()) (list-ref y 100) --> 1 y --> ((a b) a b) z --> ((a b) a b c) (list-ref y 101) --> 2 Fall 2008 Programming Development 5 Fall 2008 Programming Development 6 Techniques Techniques 1

  2. Append! Examples Appending (with mutation) (define x (list 'a 'b)) ; Exercise 3.12 (define y (list 'c 'd)) ; append! is a destructive version of append (define z (append x y)) ; note x must not be null! > z (define (append! x y) (a b c d) (set-cdr! (last-pair x) y) > x x) (a b) ; takes a non-null list and returns its last pair > (define w (append! x y)) (define (last-pair x) > w (if (null? (cdr x)) (a b c d) x > x (last-pair (cdr x)))) (a b c d) Fall 2008 Programming Development 7 Fall 2008 Programming Development 8 Techniques Techniques Copy-List Copy-List Examples ; takes a list and creates a copy (sort-of) > x9 (define (copy-list x) ((a b) c) (if (null? x) > y9 () ((a b) c) (cons (car x) (copy-list (cdr x))))) > (set-car! (car x9) 'wow) > x9 (define x9 '((a b) c)) ((wow b) c) (define y9 (copy-list x9)) > y9 ((wow b) c) Fall 2008 Programming Development 9 Fall 2008 Programming Development 10 Techniques Techniques Deep-Copy-List Deep-copy-list Example ; does a deep-copy instead of just top-level > x9-2 (define (deep-copy-list x) ((a b) c) (cond ((null? x) ()) > y9-2 ((pair? (car x)) ((a b) c) (cons (deep-copy-list (car x)) > (set-car! (car x9-2) 'wow) (deep-copy-list (cdr x)))) > x9-2 (else ((wow b) c) (cons (car x) (copy-list (cdr x)))))) > y9-2 ((a b) c) (define x9-2 '((a b) c)) (define y9-2 (deep-copy-list x9-2)) Fall 2008 Programming Development 11 Fall 2008 Programming Development 12 Techniques Techniques 2

  3. Message Passing – cons/car/cdr Testing – Message Passing (define (scons x y) (define (dispatch m) (define t1 (scons 'a '(b))) (cond ((eq? m 'scar) x) ((eq? m 'scdr) y) (check-expect (scar t1) 'a) (else (error "Undefined operation -- SCONS" m)))) (check-expect (scdr t1) '(b)) dispatch) (define (scar z) (z 'scar)) (define (scdr z) (z 'scdr)) Fall 2008 Programming Development 13 Fall 2008 Programming Development 14 Techniques Techniques Adding set-car! and set-cdr!? (define (mcons x y) (define (set-x! v) (set! x v)) (define (set-y! v) (set! y v)) (define (dispatch m) (cond ((eq? m 'mcar) x) ((eq? m 'mcdr) y) ((eq? m 'mset-car!) set-x!) ((eq? m 'mset-cdr!) set-y!) (else (error "Undefined operation -- MCONS" m)))) dispatch) Fall 2008 Programming Development 15 Fall 2008 Programming Development 16 Techniques Techniques Drawing Environment Model? > (define x (cons 1 2)) (define (mcar z) (z 'mcar)) > (define z (cons x x)) (define (mcdr z) (z 'mcdr)) > (define x (mcons 1 2)) > (define z (mcons x x)) (define (mset-car! z new-value) > (mset-car! (mcdr z) 17) ((z 'mset-car!) new-value) #<procedure:dispatch> z) (define (mset-cdr! z new-value) > (mcar x) ((z 'mset-cdr!) new-value) 17 z) Fall 2008 Programming Development 17 Fall 2008 Programming Development 18 Techniques Techniques 3

  4. Queues Queues (define q (make-queue)) • Constructor: (make-queue) (insert-queue! q ‘a) a • Predicate: (empty-queue? <queue>) (insert-queue! q ‘b) a b • Selector: (front-queue <queue>) (delete-queue! q) b • Mutators: (insert-queue! q ‘c) b c (insert-queue! <queue> <item>) (delete-queue! <queue>) (insert-queue! q ‘d) b c d (delete-queue! q) c d Fall 2008 Programming Development 19 Fall 2008 Programming Development 20 Techniques Techniques Queue Implementaton Representation of queues • Representation of queue is a list and a pair of pointers • Straightforward queue representation: simple list into that list structure • Front-queue? • Car of pair points to front of queue, where items are removed (pointer to list) • Insert-queue? • Cdr of pair points to the end of the queue, where new • Problem? items are added (pointer to last pair in list) Fall 2008 Programming Development 21 Fall 2008 Programming Development 22 Techniques Techniques Implementation of queue Queue Implementation ; makes an empty queue. A queue is a pair with a front ; pointer and a rear pointer (define (make-queue) (cons () ())) ; takes a queue and is #t if the queue is empty (define (empty-queue? q) (null? (car q))) ; takes a queue and returns the front element ; or error if the queue is empty (define (front-queue q) (if (empty-queue? q) (error "FRONT on empty queue" q) (caar q))) Fall 2008 Programming Development 23 Fall 2008 Programming Development 24 Techniques Techniques 4

  5. insert Delete ; takes a queue and an item and changes the queue ; so as to add the new item (to the end) ; takes a queue and changes it so as to delete the (define (insert-queue! q item) ; front element or creates an error if the queue (let ((new-pair (cons item ()))) ; is empty (cond ((empty-queue? q) (define (delete-queue! q) (set-car! q new-pair) (cond ((empty-queue? q) (set-cdr! q new-pair) (error "DELETE! on empty queue" q)) q) (else (else (set-car! q (cdar q))))) (set-cdr! (cdr q) new-pair) (set-cdr! q new-pair) q)))) Fall 2008 Programming Development 25 Fall 2008 Programming Development 26 Techniques Techniques Association lists Association Table (one dimensional) ((key1 . value1) (key2 . value2) ...) ; takes a key and an association list and returns ; the pair including the key in the list (define (assoc key assoc-list) (cond ((null? assoc-list) #f) ((equal? key (caar assoc-list)) (car assoc-list)) (else (assoc key (cdr assoc-list))))) Fall 2008 Programming Development 27 Fall 2008 Programming Development 28 Techniques Techniques One-dimensoinal tables Insert into table ; takes a key with a value and inserts Use tagged association lists ; it into the table table, value returned is ; makes an empty table ; the symbol done (define (make-table) (list '*table*)) (define (insert! key value table) (let ((record (assoc key (cdr table)))) ; finds an entry in the table and returns the value (if record (define (lookup key table) (set-cdr! record value) (let ((record (assoc key (cdr table)))) (set-cdr! table (if record (cons (cons key value) (cdr record) (cdr table))))) #f))) 'done) Fall 2008 Programming Development 29 Fall 2008 Programming Development 30 Techniques Techniques 5

  6. Two Dimensional Tables Two-dimensional tables Use a table of tables, with tags as keys identifying each subtable (define (make-table2) (list '*table*)) ; takes two keys and a two dimensional table ; looks up the value associated with those ; two keys in the table (define (lookup2 key1 key2 table) (let ((subtable (assoc key1 (cdr table)))) (if subtable (let ((record (assoc key2 (cdr subtable)))) (if record (cdr record) #f)) #f))) Fall 2008 Programming Development 31 Fall 2008 Programming Development 32 Techniques Techniques insert continued ; insert a new value into a table with two keys (set-cdr! table ; first check keys already exist (cons (list key1 (define (insert2! key1 key2 val table) (cons key2 val)) (let ((subtbl (assoc key1 (cdr table)))) (cdr table))))) (if subtbl 'done) (let ((record (assoc key2 (cdr subtbl)))) (if record (set-cdr! record val) (set-cdr! subtbl (cons (cons key2 val) (cdr subtbl))))) Fall 2008 Programming Development 33 Fall 2008 Programming Development 34 Techniques Techniques Multidimensional tables Implementation (discrimination nets) (define (make-table) (list '*table* #f)) • Multidimensional table is actually a tree (define (lookup key-list table) (if (null? key-list) • Each node of tree looks like (cadr table) (key value table table ...) (let ((subtbl (assoc (car key-list) (cddr table)))) • The value part of node is the value associated (if subtbl with the list of keys starting from the root of (lookup (cdr key-list) subtbl) the tree (excluding '* table* ) to this node #f)))) • I f value = #f , there is no stored value for the list of keys Fall 2008 Programming Development 35 Fall 2008 Programming Development 36 Techniques Techniques 6

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend