pairs and lists. data abstraction. sicp: sections 2.1.1 – 2.2.1 lecture notes: chapter 3

41
1 Pairs and Lists. Data Abstraction. SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

Upload: becka

Post on 22-Jan-2016

26 views

Category:

Documents


0 download

DESCRIPTION

Pairs and Lists. Data Abstraction. SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3. a. 2. 1. Box and Pointer Diagram. (define a (cons 1 2)). A pair can be implemented directly using two “pointers”. Originally on IBM 704: (car a) C ontents of A ddress part of R egister - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Pairs and Lists. Data Abstraction.  SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

1

Pairs and Lists.

Data Abstraction.

SICP: Sections 2.1.1 – 2.2.1

Lecture notes: Chapter 3

Page 2: Pairs and Lists. Data Abstraction.  SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

2

Box and Pointer Diagram

A pair can be implemented directly using two “pointers”.

Originally on IBM 704:

(car a) Contents of Address part of Register

(cdr a) Contents of Decrement part of Register

(define a (cons 1 2))2

1

a

Page 3: Pairs and Lists. Data Abstraction.  SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

3

Pair: A primitive data type.

Constructor: (cons a b)

Selectors: (car p)

(cdr p)

Guarantee: (car (cons a b)) = a

(cdr (cons a b)) = b

Abstraction barrier: We say nothing about the representation or implementation of pairs.

Page 4: Pairs and Lists. Data Abstraction.  SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

4

Pairs

(define x (cons 1 2))

(define y (cons 3 4))

(define z (cons x y))

(car (car z)) 1 ;(caar z)

(car (cdr z)) 3 ;(cadr z)

Page 5: Pairs and Lists. Data Abstraction.  SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

5

Box and pointer diagrams

4

1

3

2

(cons (cons 1 (cons 2 3)) 4)

Page 6: Pairs and Lists. Data Abstraction.  SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

6

Compound Data

A closure property: The result obtained by creating a compound data structure can itself be treated as a primitive object and thus be input to the creation of another compound object.

Pairs have the closure property:

We can pair pairs, pairs of pairs etc.

(cons (cons 1 2) 3)

3

2

1

Page 7: Pairs and Lists. Data Abstraction.  SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

7

Lists

(cons 1 (cons 3 (cons 2 ’() )))

1 3 2

Syntactic sugar: (list 1 3 2)

The empty list (a.k.a. null or nill)

Page 8: Pairs and Lists. Data Abstraction.  SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

7מבוא מורחב שיעור 8

Formal Definition of a List

A list is either

• ’() -- The empty list• A pair whose cdr is a list.

Lists are closed under the operations cons and cdr:

• If lst is a non-empty list, then (cdr lst) is a list.

• If lst is a list and x is arbitrary, then (cons x lst) is a list.

Page 9: Pairs and Lists. Data Abstraction.  SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

9

Lists

(list <x1> <x2> ... <xn>)

is syntactic sugar for

(cons <x1> (cons <x2> ( … (cons <xn> ’() ))))

<x1> <x2> <xn>

Page 10: Pairs and Lists. Data Abstraction.  SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

10

Lists (examples)

(cdr (list 1 2 3))

(cdr (cons 1 (cons 2 (cons 3 ’() ))))

(cons 2 (cons 3 ’() ))

(list 2 3)

(cons 3 (list 1 2))(cons 3 (cons 1 (cons 2 ’() ))) (list 3 1 2)

3 1 2

2 3

The following expressions all result in the same structure:

and similarly the following

Page 11: Pairs and Lists. Data Abstraction.  SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

12

More Elaborate Lists(list 1 2 3 4)

(cons (list 1 2) (list 3 4))

(list (list 1 2) (list 3 4))

1 2 3 4

1

3 4

2

1 3 42

Value:(1 2 3 4)

Value:((1 2) 3 4)

Value:((1 2) (3 4))

Page 12: Pairs and Lists. Data Abstraction.  SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

List of Symbols (shorthand)

• ‘(a b c) translates into (‘a ‘b ‘c)

13

Page 13: Pairs and Lists. Data Abstraction.  SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

14

Yet More Examples

p2

( (1 . 2) (1 . 2) )

1 2

p

3

p1

p2

(define p (cons 1 2))

p

(1 . 2)

(define p1 (cons 3 p)

p1

(3 1 . 2)

(define p2 (list p p))

Page 14: Pairs and Lists. Data Abstraction.  SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

15

The Predicate Null?

null? : anytype -> boolean

(null? <z>) #t if <z> evaluates to empty list

#f otherwise

(null? 2) #f

(null? (list 1)) #f

(null? (cdr (list 1))) #t

(null? ’()) #t

(null? null) #t

Page 15: Pairs and Lists. Data Abstraction.  SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

16

The Predicate Pair?

pair? : anytype -> boolean

(pair? <z>) #t if <z> evaluates to a pair

#f otherwise.

(pair? (cons 1 2)) #t

(pair? (cons 1 (cons 1 2))) #t

(pair? (list 1)) #t

(pair? ’()) #f

(pair? 3) #f

(pair? pair?) #f

Page 16: Pairs and Lists. Data Abstraction.  SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

17

The Predicate Atom?atom? : anytype -> boolean

(define (atom? z) (and (not (pair? z)) (not (null? z))))

(define (square x) (* x x))(atom? square) #t (atom? 3) #t(atom? (cons 1 2)) #f

Page 17: Pairs and Lists. Data Abstraction.  SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

18

More examples

(define digits (list 1 2 3 4 5 6 7 8 9))

?(0 1 2 3 4 5 6 7 8 9)

(define digits1 (cons 0 digits))digits1

(define l (list 0 digits))l?(0 (1 2 3 4 5 6 7 8 9))

Page 18: Pairs and Lists. Data Abstraction.  SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

19

The procedure length(define digits (list 1 2 3 4 5 6 7 8 9))(length digits)

9(define l null)(length l)0

(define l (cons 1 l))(length l)1

(define (length l) (if (null? l) 0

(+ 1 (length (cdr l)))))

Page 19: Pairs and Lists. Data Abstraction.  SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

The procedure append

(define (append list1 list2) (cond ((null? list1) list2) ; base (else (cons (car list1) ; recursion (append (cdr list1) list2)))))

20

Page 20: Pairs and Lists. Data Abstraction.  SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

Constructor

• Type: Number * T -> LIST(T)

> (make-list 7 ’foo)

(foo foo foo foo foo foo foo)

> (make-list 5 1)

(1 1 1 1 1)

21

Page 21: Pairs and Lists. Data Abstraction.  SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

List type

• Pairs:• For every type assignment TA and type expressions

S,S1,S2:• TA |- cons:[S1*S2 -> PAIR(S1,S2)]• TA |- car:[PAIR(S1,S2) -> S1]• TA |- cdr:[PAIR(S1,S2) -> S2]• TA |- pair?:[S -> Boolean]• TA |- equal?:[PAIR(S1,S2)*PAIR(S1,S2) -> Boolean]

22

Page 22: Pairs and Lists. Data Abstraction.  SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

For every type environment TEnv and type expression S:

TEnv |- list:[Unit -> LIST(S)]

TEnv |- cons:[T*LIST(S) -> LIST(S)]

TEnv |- car:[LIST(S) -> S]

TEnv |- cdr:[LIST(S) -> LIST(S)]

TEnv |- null?:[LIST(S) -> Boolean]

TEnv |- list?:[S -> Boolean]

TEnv |- equal?:[LIST(S)*LIST(S) -> Boolean]

23

Page 23: Pairs and Lists. Data Abstraction.  SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

Cont.

• For every type environment TEnv and type expression S:• TEnv |- list:[Unit -> LIST]• TEnv |- cons:[S*LIST -> LIST]• TEnv |- car:[LIST -> S]• TEnv |- cdr:[LIST -> LIST]• TEnv |- null?:[LIST -> Boolean]• TEnv |- list?:[S -> Boolean]• TEnv |- equal?:[LIST*LIST -> Boolean]

24

Page 24: Pairs and Lists. Data Abstraction.  SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

25

Procedural abstraction

Export only what is needed.

• Publish: name, number and type of arguments (and conditions they must satisfy) type of procedure’s return value• Guarantee: the behavior of the procedure

• Hide: local variables and procedures, way of implementation, internal details, etc.

Interface

Implementation

Page 25: Pairs and Lists. Data Abstraction.  SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

26

Data-object abstraction

Export only what is needed.

• Publish: constructors, selectors

• Guarantee: the behavior

• Hide: local variables and procedures, way of implementation, internal details, etc.

Interface

Implementation

Page 26: Pairs and Lists. Data Abstraction.  SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

27

An example: Rational numbers

We would like to represent rational numbers.A rational number is a quotient a/b of two integers.

Constructor: (make-rat a b)

Selectors: (numer r)

(denom r)

Guarantee: (numer (make-rat a b)) = a

(denom (make-rat a b)) = b

Page 27: Pairs and Lists. Data Abstraction.  SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

28

An example: Rational numbers

We would like to represent rational numbers.A rational number is a quotient a/b of two integers.

Constructor: (make-rat a b)

Selectors: (numer r)

(denom r)

A betterGuarantee:

(numer (make-rat a b))

(denom (make-rat a b))= a

b

A weaker condition, but still sufficient!

Page 28: Pairs and Lists. Data Abstraction.  SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

29

(add-rat x y) (sub-rat x y) (mul-rat x y) (div-rat x y) (equal-rat? x y) (print-rat x)

We can now use the constructors and selectors to implement operations on rational numbers:

A form of wishful thinking: we don’t know how make-rat numer and denom are implemented, but we use them.

Page 29: Pairs and Lists. Data Abstraction.  SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

30

(define (equal-rat? x y) (= (* (numer x) (denom y)) (* (numer y) (denom x))))

Implementing the operations

(define (mul-rat x y) (make-rat (* (numer x) (numer y)) (* (denom x) (denom y))))

(define (div-rat x y) (make-rat (* (numer x) (denom y)) (* (denom x) (numer y))))

(define (sub-rat x y) …

(define (add-rat x y) ;n1/d1 + n2/d2 = (n1.d2 + n2.d1) / (d1.d2) (make-rat (+ (* (numer x) (denom y)) (* (numer y) (denom x))) (* (denom x) (denom y))))

Page 30: Pairs and Lists. Data Abstraction.  SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

31

Using the rational package

(define (print-rat x) (newline) (display (numer x)) (display ”/”) (display (denom x)))

(define one-half (make-rat 1 2))

(print-rat one-half) 1/2

(define one-third (make-rat 1 3))

(print-rat (add-rat one-half one-third)) 5/6

(print-rat (add-rat one-third one-third)) 6/9

Page 31: Pairs and Lists. Data Abstraction.  SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

32

Abstraction barriers

Programs that use rational numbers

add-rat sub-rat mul-rat…

make-rat numer denom

rational numbers in problem domain

rational numbers as numerators and denumerators

Page 32: Pairs and Lists. Data Abstraction.  SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

33

Gluing things together

We still have to implement numer, denom, and make-rat

A pair:

We need a way to glue things together…

(define x (cons 1 2))

(car x) 1

(cdr x) 2

Page 33: Pairs and Lists. Data Abstraction.  SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

34

Implementing make-rat, numer, denom

(define (make-rat n d) (cons n d))

(define (numer x) (car x))

(define (denom x) (cdr x))

Page 34: Pairs and Lists. Data Abstraction.  SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

35

Abstraction barriers

Programs that use rational numbers

add-rat sub-rat mul-rat...

make-rat numer denom

cons car cdr

rational numbers in problem domain

rational numbers as numerators and denumerators

rational numbers as pairs

Page 35: Pairs and Lists. Data Abstraction.  SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

36

Alternative implementation for add-rat

(define (add-rat x y) (cons (+ (* (car x) (cdr y)) (* (car y) (cdr x))) (* (cdr x) (cdr y))))

AbstractionViolation

If we bypass an abstraction barrier,changes to one level may affect many levels above it.Maintenance becomes more difficult.

Page 36: Pairs and Lists. Data Abstraction.  SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

37

A solution: change the constructor

(define (make-rat a b) (let ((g (gcd a b))) (cons (/ a g) (/ b g))))

In our current implementation we keep 10000/20000as such and not as 1/2.

This: • Makes the computation more expensive.• Prints out clumsy results.

No other changes are required!

Rationals - Alternative Implementation

Page 37: Pairs and Lists. Data Abstraction.  SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

38

Reducing to lowest terms, another way

(define (make-rat n d) (cons n d))

(define (numer x) (let ((g (gcd (car x) (cdr x)))) (/ (car x) g)))

(define (denom x) (let ((g (gcd (car x) (cdr x)))) (/ (cdr x) g)))

Page 38: Pairs and Lists. Data Abstraction.  SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

39

How can we implement pairs? (first solution – “lazy” implementation)

(define (cons x y) (lambda (f) (f x y)))

(define (car z) (z (lambda (x y) x)))

(define (cdr z) (z (lambda (x y) y)))

Page 39: Pairs and Lists. Data Abstraction.  SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

40

(define (cons x y) (lambda (f) (f x y)))

(define (car z) (z (lambda (x y) x)))

(define (cdr z) (z (lambda (x y) y)))

How can we implement pairs? (first solution, cont’)

> (define p (cons 1 2))Name Value

(lambda(f) (f 1 2)) p

> (car p)

( (lambda(f) (f 1 2)) (lambda (x y) x))

( (lambda(x y) x) 1 2 )

> 1

Page 40: Pairs and Lists. Data Abstraction.  SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

41

How can we implement pairs?(Second solution: “eager” implementation)

(define (cons x y) (lambda (m) (cond ((= m 0) x) ((= m 1) y) (else (error "Argument not 0 or

1 -- CONS" m))))))

(define (cdr z) (z 1))

(define (car z) (z 0))

Page 41: Pairs and Lists. Data Abstraction.  SICP: Sections 2.1.1 – 2.2.1 Lecture notes: Chapter 3

42

(define (cons x y) (lambda (m) (cond ((= m 0) x) ((= m 1) y) (else ...))) (define (car z) (z 0))

(define (cdr z) (z 1))

Implementing pairs (second solution, cont’)

> (define p (cons 3 4)) (lambda(m) (cond ((= m 0) 3) ((= m 1) 4) (else ..)))

p

> (car p)

((lambda(m) (cond ..)) 0)

(cond ((= 0 0) 3) ((= 0 1) 4) (else ...)))

> 3

Name Value