day17

day17 - CS1: Introduction to Computation Day 17: November...

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

View Full Document Right Arrow Icon
Caltech CS 1 -- Fall 2008 CS1: Introduction to Computation Day 17: November 24, 2008 Mutable Data Structures [and extra goodies]
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 4
Caltech CS 1 -- Fall 2008 Simple example 3 (define a (cons 1 2)) a (1 . 2) ( set-car! a 3) a (3 . 2)
Background image of page 5

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

View Full DocumentRight Arrow Icon
Caltech CS 1 -- Fall 2008 Similarly a (3 . 2) ( set-cdr! a 10) a (3 . 10) 10
Background image of page 6
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
Background image of page 7

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

View Full DocumentRight Arrow Icon
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!
Background image of page 8
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?
Background image of page 9

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

View Full DocumentRight Arrow Icon
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
Background image of page 10
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!
Background image of page 11

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

View Full DocumentRight Arrow Icon
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)))))
Background image of page 12
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
Background image of page 13

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

View Full DocumentRight Arrow Icon
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)
Background image of page 14
Caltech CS 1 -- Fall 2008 append
Background image of page 15

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

View Full DocumentRight Arrow Icon
Caltech CS 1 -- Fall 2008 append
Background image of page 16
Caltech CS 1 -- Fall 2008 append
Background image of page 17

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

View Full DocumentRight Arrow Icon
Caltech CS 1 -- Fall 2008 append
Background image of page 18
Caltech CS 1 -- Fall 2008 append
Background image of page 19

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

View Full DocumentRight Arrow Icon
Caltech CS 1 -- Fall 2008 append
Background image of page 20
Image of page 21
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 109

day17 - CS1: Introduction to Computation Day 17: November...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online