day17

# day17 - CS1 Introduction to Computation Day 17 Mutable Data...

This preview shows pages 1–21. Sign up to view the full content.

Caltech CS 1 -- Fall 2008 CS1: Introduction to Computation Day 17: November 24, 2008 Mutable Data Structures [and extra goodies]

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Caltech CS 1 -- Fall 2008 Four lectures ago… • Introduced one Scheme command set! • which forced us to adopt a new model – gave us new power – potentially complicated programs
Caltech CS 1 -- Fall 2008 Today • Complete the introduction of side effects – Introduce two new procedures: set-car! and set-cdr! – Allow us to mutate data structures • shape and contents • Force us to think (more) carefully about – sharing of data – equality

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Caltech CS 1 -- Fall 2008 set-car! and set-cdr! set-car! – allows us to change the value associated with the car of a cons cell set-cdr! – allows us to change the value associated with the cdr of a cons cell
Caltech CS 1 -- Fall 2008 Simple example 3 (define a (cons 1 2)) a (1 . 2) ( set-car! a 3) a (3 . 2)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Caltech CS 1 -- Fall 2008 Similarly a (3 . 2) ( set-cdr! a 10) a (3 . 10) 10
Caltech CS 1 -- Fall 2008 Locations • Recall that we needed to introduce the notion of a location to reason about the environment model – allowed us to reason about changing the value associated with a location • cons cell is really a pair of locations • set-car! and set-cdr! – allow us to change the value associated with the car and cdr locations

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Caltech CS 1 -- Fall 2008 Evaluation • set-car! is not a special form – To evaluate ( set-car! exp1 exp2) • evaluate the operands: exp1 , exp2 • exp1 must be a cons pair • replace the car value of the exp1 pair – with the value of exp2 • Similarly for set-cdr!
Caltech CS 1 -- Fall 2008 Common pitfall! • This is not legal Scheme code: ;; assume x is a cons pair (set! (car x) 10) ;; wrong! (set! (cdr x) 20) ;; wrong! • Why not? – hint: what kind of thing is the first operand?

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Caltech CS 1 -- Fall 2008 Common pitfall! ;; assume x is a cons pair (set! (car x) 10) set!: not an identifier in: (car x) • set! must have a symbol as first operand • a cons pair is not a symbol • Probably wanted to write (set-car! x 10) • set! is not "smart" about its operands
Caltech CS 1 -- Fall 2008 Common pitfall! • Every year, many students write (set! (car x) 10) or (set! (cdr x) 20) on final exam when they mean (set-car! x 10) or (set-cdr! x 20) • Do this, and you will lose lots of marks • You have been warned!

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Caltech CS 1 -- Fall 2008 Simple example • Appending two lists • Built-in function append • Can define as follows: (define (append lst1 lst2) (cond ((null? lst1) lst2) ;; base cases ((null? lst2) lst1) (else (cons (car lst1) ;; recursive case (append (cdr lst1) lst2)))))
Caltech CS 1 -- Fall 2008 append • Does not change contents of lists • Does not change original lists • Attaches a new copy of lst1 onto the front of lst2

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Caltech CS 1 -- Fall 2008 append (define lst1 (list 1 2 3 4)) (define lst2 (list 5 6 7 8)) (define result ( append lst1 lst2)) result (1 2 3 4 5 6 7 8) lst1 (1 2 3 4) lst2 (5 6 7 8)
Caltech CS 1 -- Fall 2008 append

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Caltech CS 1 -- Fall 2008 append
Caltech CS 1 -- Fall 2008 append

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Caltech CS 1 -- Fall 2008 append
Caltech CS 1 -- Fall 2008 append

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Caltech CS 1 -- Fall 2008 append
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 109

day17 - CS1 Introduction to Computation Day 17 Mutable Data...

This preview shows document pages 1 - 21. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online