Title: Data Abstraction.
1Lecture 7
- Data Abstraction.
- Pairs and Lists.
- (Sections 2.1.1 2.2.1)
2Last Lecture Finding the smallest divisor
(define (divides? a b) ( (remainder b a) 0))
(define (find-smallest-divisor n i) (cond
((divides? i n) i) (else (find-smallest-divis
or n ( i 1)))))
- (define (prime? n)
- ( n (find-smallest-divisor n 2)))
Space complexity is ?(1)For prime n we have
time complexity n.
If n is a 100 digit number we will wait forever.
3An improvement
(define (divides? a b) ( (remainder b a) 0))
(define (find-smallest-divisor n i) (cond ((gt
(square i) n) n) ((divides? i n) i)
(else (find-smallest-divisor n ( i 1)))))
- (define (prime? n) ( n (find-smallest-divisor
n 2)))
For prime n we have time complexity
?(?n)Worst case space complexity ?(1)
Still, if n is a 100 digit number, it is
completely infeasible.
4Fast computation of modular exponentiationab mod
m
- (define (expmod a b m)
- (cond
- (( b 0) 1)
- ((even? b)
- (remainder (expmod
- (remainder ( a a) m)
- (/ b 2)
- m)
- m))
- (else
- (remainder ( a (expmod a (- b 1) m))
- m))))
5Procedural abstraction
- Publish name, number and type of
arguments - (and conditions they must
satisfy) - type of procedures return
value - Guarantee the behavior of the procedure
- Hide local variables and procedures,
- way of implementation,
- internal details, etc.
- Export only what is needed.
6Data-object abstraction
- Publish constructors, selectors
- Guarantee the behavior
- Hide local variables and procedures,
- way of implementation,
- internal details, etc.
- Export only what is needed.
7An 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
8An 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))
a
(denom (make-rat a b))
b
A weaker condition, but still sufficient!
9We can now use the constructors and selectors to
implement operations on rational numbers
(add-rat x y) (sub-rat x y) (mul-rat x y)
(div-rat x y) (equal-rat? x y) (print-rat x)
A form of wishful thinking we dont know how
make-rat numer and denom are implemented, but we
use them.
10Implementing the operations
(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))))
(define (sub-rat x y)
(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 (equal-rat? x y) ( ( (numer x)
(denom y)) ( (numer y) (denom x))))
11Using 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
12Abstraction barriers
rational numbers in problem domain
rational numbers as numerators and denumerators
13Gluing things together
We still have to implement numer, denom, and
make-rat
We need a way to glue things together
A pair
(define x (cons 1 2))
(car x) ? 1
(cdr x) ? 2
14Pair 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.
15Pairs
(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)
16Implementing make-rat, numer, denom
(define (make-rat n d) (cons n d)) (define
(numer x) (car x)) (define (denom x) (cdr x))
17Abstraction barriers
rational numbers in problem domain
rational numbers as numerators and denumerators
rational numbers as pairs
18Alternative implementation for add-rat
(define (add-rat x y) (cons ( ( (car x) (cdr
y)) ( (car y) (cdr x))) (
(cdr x) (cdr y))))
If we bypass an abstraction barrier, changes to
one level may affect many levels above
it. Maintenance becomes more difficult.
19Rationals - Alternative Implementation
- In our current implementation we keep 10000/20000
- as such and not as 1/2.
- This
- Makes the computation more expensive.
- Prints out clumsy results.
A solution change the constructor (define
(make-rat a b) (let ((g (gcd a b)))
(cons (/ a g) (/ b g))))
No other changes are required!
20Reducing 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)))
21How can we implement pairs? (first solution)
(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)))
22How can we implement pairs? (first solution,
cont)
gt (define p (cons 1 2))
gt (car p)
(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)))
23How can we implement pairs?(Second solution
message passing)
(define (cons x y) (lambda (m) (cond (( m
0) x) (( m 1) y) (else
(error "Argument not 0 or
1 -- CONS" m))))))
(define (car z) (z 0))
(define (cdr z) (z 1))
24Implementing pairs (second solution, cont)
Name Value
gt (define p (cons 3 4))
p
(lambda(m) (cond (( m 0) 3) (( m 1)
4) (else ..)))
gt (car p)
(define (cons x y) (lambda (m) (cond (( m
0) x) (( m 1) y) (else
...)))
(define (car z) (z 0))
(define (cdr z) (z 1))
25Implementation of Pairs -The way it is really
done
- Scheme provides an implementation of pairs, so
we do not need to use these clever
implementations. - The natural implementation is by using storage.
- The two solutions we presented show that the
distinction between storage and computation is
not always clear. - Sometimes we can trade data for computation.
- The solutions we showed have their own
significance - The first is used to show that lambda calculus
can simulate other models of computation
(theoretical importance). - The second message passing is the basis for
Object Oriented Programming. We will return to it
later.
26Box and Pointer Diagram
(define a (cons 1 2))
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
27Box and pointer diagrams (cont.)
(cons (cons 1 (cons 2 3)) 4)
28Compound 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.
29Lists
The empty list (a.k.a. null or nill)
(cons 1 (cons 3 (cons 2 () )))
Syntactic sugar (list 1 3 2)
30 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.
31 Lists
- (list ltx1gt ltx2gt ... ltxngt)
- is syntactic sugar for
- (cons ltx1gt (cons ltx2gt ( (cons ltxngt () ))))
32 Lists (examples)
The following expressions all result in the same
structure
(cons 3 (list 1 2)) (cons 3 (cons 1 (cons 2 ()
))) (list 3 1 2)
and similarly the following
- (cdr (list 1 2 3))
- (cdr (cons 1 (cons 2 (cons 3 () ))))
- (cons 2 (cons 3 () ))
- (list 2 3)
33Further List Operations
(define one-to-four (list 1 2 3 4)) one-to-four
gt (1 2 3 4) (1 2 3 4) gt
error
(car one-to-four) gt
1
2
(car (cdr one-to-four)) gt
(cadr one-to-four) gt
2
3
(caddr one-to-four) gt
34 More Elaborate Lists
- (list 1 2 3 4)
- (cons (list 1 2) (list 3 4))
- (list (list 1 2) (list 3 4))
- Prints as (1 2 3 4)
- Prints as ((1 2) 3 4)
- Prints as ((1 2) (3 4))
35 Yet More Examples
36 The Predicate Null?
- null? anytype -gt boolean
- (null? ltzgt)
- t if ltzgt evaluates to empty list
- f otherwise
- (null? 2) ? f
- (null? (list 1)) ? f
- (null? (cdr (list 1))) ? t
- (null? ()) ? t
- (null? null) ? t
37 The Predicate Pair?
- pair? anytype -gt boolean
- (pair? ltzgt) t if ltzgt 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
38 The Predicate Atom?
- atom? anytype -gt 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
39More examples
- (define digits (list 1 2 3 4 5 6 7 8 9))
?
(0 1 2 3 4 5 6 7 8 9)
40The procedure length
- (define digits (list 1 2 3 4 5 6 7 8 9))
- (length digits)
- (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)))))