6.001 Recitation #4 – Feb 14, 2003

 

RI: Konrad Tollmar

www.ai.mit.edu/6001

 

•Pairs and lists

•Common list operations

•Data abstractions

 

 

1. Printed representation.

(define x (list 1 2))

(define y (list (list 1 2) (list 1 2))

(define z (list x x))

 

 

X ==>

 

Y ==>

 

Z ==>

 

 

2. cons, car, cdr.

(define thing (cons (cons 1 nil) (cons 2 (cons 3 nil))))

 

thing ==>

 

(cons 1 nil) ==>

(cons 1 (cons 2 nil)) ==>

(car thing) ==>

(cdr thing) ==>

(car (car thing)) ==>

(car (cdr (cdr thing))) ==>

 

(car (cons (+ 1 2) (- 3 4)))  ==>      

(cdr 6) ==>

(cdr (car (cons (cons 1 2) (cons 3 4))))

(pair? #t)  ==>

(pair? (car (cons 1 2))) ==>

(pair? (cons (+ 1 2) (car (cons (3 4))))) ==>

3. lenght, list-ref, append.

x => (())

y => (1 2 3)

z => (1 (2 3) ((4)))

w => (1 2 3 4 5)

 

 

(length x)

(length y)

(length z)

(list-ref z 2)

(append x y)

(cons x y)


4. Try substitution on a list procedure.

(define (map proc lst)

  (if (null? lst)

      nil

      (cons (proc (car lst))

            (map proc (cdr lst)))))

 

 

(map square (1 2 3))

 

5. Data abstractions

 

You and your partner have been asked to build a small vector package. Firstly should you together define an interface to this package, i.e. the constructor, selectors and operations. Secondly, one of you implements the constructors and the selectors, while your partner implement a couple of vector operations. Last verify that your code work together and prepare a brief presentation.

 

1. Constructor

 

 

2. Accessors

 

 

3. Contract

 

 

4. Layered Operations

 

 

5. Abstraction Barrier

 

 

6. Concrete Representation & Implementation