| 95.307 - Programming Paradigms | 1999 |
4 Building Abstractions with Data |
| 4.1 Introduction |
| 4.2 Abstraction |
n1/d1 + n2/d2 = (n1d2 + n2d1) / (d1d2)
n1/d1 - n2/d2 = (n1d2 - n2d1) / (d1d2)
n1/d1 * n2/d2 = (n1n2) / (d1d2)
(n1/d1)/ (n2d2) = (n1d2) / (n2d1)
n1/d1 = n2/d2 if and only n1d2 = n2d1
(numerator ?x) returns numerator of rational number ?x
(denominator ?x) returns denominator of rational number ?x
(define (add-rational x y)
(make-rational (+ (* (numerator x)
(denominator y))
(* (numerator y) (denominator x)))
(* (denominator x) (denominator y))))
(define (subtract-rational x y)
(make-rational (- (* (numerator x)
(denominator y))
(* (numerator y) (denominator x)))
(* (denominator x) (denominator y))))
(define (multiply-rational x y)
(make-rational (* (numerator x) (numerator
y))
(* (denominator x) (denominator y))))
(define (divide-rational x y)
(make-rational (* (numerator x) (denominator
y))
(* (denominator x) (numerator y))))
(define (equal?-rational x y)
(= (* (numerator x) (denominator y))
(* (numerator
y) (denominator x))))
| 4.3 Pairs |
(cons 1 2) => (1.2) // . only for display
(define x (cons 1 2))
(car x) => 1
(cdr x) => 2
(define y ( cons 3 4))
(cons x y) => ((1.2) 3 . 4) //Think of pairs as a single object
(define z (cons x y))
(car z) => (1.2)
(car (car z)) => 1
(car (cdr z)) => 3
(define one-third (make-rational 1 3))
(add-rational one-third one-third) => (6.9)
Must fix problems with display
(define (print-rational x)
- should not show dot notion
- should reduce rational to lowest terms
(newline)
(display (numerator x))
(display "/")
(display (denominator x)))(print-rational (add-rational one-third one-third)) => (6 / 9)
(define (make-rational numerator denominator)
(let (( g (gcd numerator denominator)))
(cons (/ numerator g) (/ denominator g))))(print-rational (add-rational one-third one-third)) => (2 / 3)
| 4.4 Abstraction Barriers |
(define (numerator rational) (let ((g (gcd (car rational) (cdr rational)))) (/ (car rational) g)))
| 4.5 What is Meant by Data |
| 4.6 Representing Sequences |
(list a1 a2 ... an) which is equivalent to (cons a1 (cons a2 ( .... (cons an '()))) ...)
- Note: #f, nil, and '() are all the same.
- DrScheme does not define nil for you. Do it using (define nil '())
(define one-to-four (list 1 2 3 4)) (car one-to-four) => 1 (cdr one-to-four) => (2 3 4) (cons 10 one-to-four) => (10 1 2 3 4)
Technique know as cdring down
Technique know as consing up
(define odds (list 1 3 5 7 9))
(append square odds) => (1 4 9 16 25 1 3 5 7 9)
(define (scale-list items factor)
(if (null? items)
nil
(cons (* (car items) factor)
(scale-list (cdr items) factor))))Technique know as mapping over lists
(scale-list (list 1 2 3 4 5 ) 10) => ( 10 20 30 40 50)
Abstracting the general idea to capture a common pattern expressed as a higher-order procedure
(define (map procedure items)
(if (null? items)
nil
(cons (procedure (car items))
(map procedure (cdr items)))))(map abs (list -10 2.5 -11.6 17)) => (10 2.5 11.6 17)
(map (lambda (x) (* x x)) (list 1 2 3 4)) => ( 1 4 9 16)
(define (filter predicate sequence)
(cond ((null? sequence) '())
((predicate (car sequence)) (cons (car sequence) (filter predicate (cdr sequence))))
(else (filter predicate (cdr sequence)))))Technique know as filtering over lists
(filter odd? (list 1 2 3 4 5 6 7)) => ( 1 3 5 7)
(define (accumulate operator initial sequence)
(if (null? sequence)
initial
(operator (car sequence) (accumulate operator initial (cdr sequence)))))Technique know as accumulating over lists
(accumulate + 0 (list 1 2 3 4 5)) => 15
(accumulate * 1 (list 1 2 3 4 5)) => 120
(define (flatten-map procedure sequence)
(accumulate append nil (map procedure sequence)))(map list (list 1 2 3)) => ( (1) (2) (3))
(flatten-map list (list 1 2 3)) => ( 1 2 3)
| 4.7 Hierarchical Structures |
| 4.8 Symbolic Data |
- Suppose we want (a b)
- Can't use (list a b) because list will evaluate a b first
- In order to manipulate symbols we need the ability to quote a data object
- Quotation marks indicate that a word or sentences is to be treated literally as a string of characters
- Scheme follows the same practice to identify lists and symbols
- The meaning of single quote character is to quote the next object
- Quote also allows us to type in compound objects
(define a 1) (define b 2) (list a b) => (1 2) (list 'a 'b) => (a b) (list a 'b) => (1 b) (list 'me 'you) => (me you) (car '(a b c)) => a (cdr '(a b c)) => (b c)
- Violates general rule that all compound expressions should be delimited by ().
- Therefore special form quote is added
- (quote a) and 'a are equivalent, as is (quote ( a b c)) and '(a b c)