Title: Dotted Tail Notation
1???? ????? ????? ?????
2- Dotted Tail Notation
- Apply
- Data Abstractions
- Sierpinski triangle
- Sets and Huffman Coding
3Writing procedures with a variable number of
parameters (dotted-tail notation)
4Writing procedures with a variable number of
parameters (dotted-tail notation)
All other parameters will be passed in a list
named z
(define (proc x y . z) ltbodygt)
5Dotted-tail notation examples
1
x ? ? y ? ? z ? ?
- (define (proc x y . z) ltbodygt)
- (proc 1 2 3 4 5 6)
2
(3 4 5 6)
null
ERROR
6Dotted-tail notation another example
- (define (proc . z) ltbodygt)
- (proc 1 2 3 4 5 6)
z ? ?
(1 2 3 4 5 6)
null
(1 (2 3))
7Dotted-tail notation another example
We want to implement a procedure (count) that
gets a variable number of parameters and returns
their number
(count 1 7 8 9 1) ? 5 (count 0) ? 1 (count) ? 0
(define (count . s) _________________)
(length s)
8Dotted-tail notation yet another example
(define (lt x . y) (length (filter (lambda (z)
(lt z x)) y)))
What does this procedure do ?
(lt 1 7 8 9 1 0 -1) ? 2 (lt 1000 1 10 0) ? 3 (lt
1) ? 0 (lt) ? ERROR
lt returns the number of parameters that are
smaller than the first parameter.
9The built-in procedure Apply
- Suppose we want to apply some procedure proc
- but the parameters are available as a list
(apply proc lst)
will give the same result as
(proc v1 v2 vn)
assuming lst is (v1 v2 vn)
10Apply examples
(apply (list 1 2 3))
? 6
(apply count (list 1 2 3))
? 3
(apply lt (list 10 1 2 3 11 9))
? 4
? (1 2 3 4)
(apply append '((1) (2) (3) (4)))
(apply expt '(2 4))
? 16
? (1 2 3 4 5)
(apply list '(1 2 3 4 5))
11General map
(define (f x y z) ( x ( 2 y) ( 5
z))) (general-map f '(1 2 3) '(2 3 4) '(2 4 7))
? (15 28 46)
(define (general-map proc list1 . other-lists)
(if (null? list1) '() (let ((lists
(cons list1 other-lists))) (let ((firsts
(simple-map car lists)) (rests
(simple-map cdr lists))) (cons (apply
proc firsts) (apply general-map
(cons proc rests)))))))
We use the names simple-map and general-map for
clarity. The general procedure is actually
called map.
12Another Apply example
Exercise Implement a procedure, add-abs, that
receives 0 or more numbers, and returns the sum
of their absolute values.
Examples(add-abs) ? 0 (add-abs 1 -2 -3 4) ? 10
(define (add-abs . l) (if (null? l) 0
( (abs (car l)) _________________________))
)
Does not work!!
(add-abs (cdr l))
13Another Apply example
Exercise Implement a procedure, add-abs, that
receives 0 or more numbers, and returns the sum
of their absolute values.
Examples(add-abs) ? 0 (add-abs 1 -2 -3 4) ? 10
(define (add-abs . l) (if (null? l) 0
( (abs (car l)) _________________________))
)
This is ok!
(apply add-abs (cdr l))
14Another Apply example (cont.)
Exercise Implement a procedure, add-abs, that
receives 0 or more numbers, and returns the sum
of their absolute values.
Examples(add-abs) ? 0 (add-abs 1 -2 -3 4) ? 10
Another solution (also using apply)
(define (add-abs . l) (apply
________________________))
(map abs l)
15Fractals
- Definitions
- A mathematically generated pattern that is
reproducible at any magnification or reduction. - A self-similar structure whose geometrical and
topographical features are recapitulated in
miniature on finer and finer scales. - An algorithm, or shape, characterized by
self-similarity and produced by recursive
sub-division.
16Sierpinski triangle
- Given the three endpoints of a triangle, draw
the triangle - Compute the midpoint of each side
- Connect these midpoints to each other, dividing
the given triangle into four triangles - Repeat the process for the three outer triangles
17Sierpinski triangle Scheme version
(define (sierpinski triangle) (cond
((too-small? triangle) t) (else
(draw-triangle triangle) (sierpinski
outer triangle 1 ) (sierpinski
outer triangle 2 ) (sierpinski
outer triangle 3 ))))
18Scheme triangle
Constructor
(define (make-triangle a b c) (list a b
c)) (define (a-point triangle) (car triangle))
(define (b-point triangle) (cadr triangle))
(define (c-point triangle) (caddr
triangle)) (define (too-small? triangle) (let
((a (a-point triangle)) (b (b-point
triangle)) (c (c-point triangle)))
(or (lt (distance a b) 2) (lt (distance b
c) 2) (lt (distance c a) 2)))) (define
(draw-triangle triangle) (let ((a (a-point
triangle)) (b (b-point triangle))
(c (c-point triangle))) (and ((draw-line
view) a b my-color) ((draw-line view) b
c my-color) ((draw-line view) c a
my-color))))
Selectors
Predicate
Draw
19Points
Constructor
(define (make-posn x y) (list x y)) (define
(posn-x posn) (car posn)) (define (posn-y posn)
(cadr posn)) (define (mid-point a b)
(make-posn (mid (posn-x a) (posn-x b))
(mid (posn-y a) (posn-y b)))) (define (mid x y)
(/ ( x y) 2)) (define (distance a b) (sqrt
( (square (- (posn-x a) (posn-x b)))
(square (- (posn-y a) (posn-y b))))))
Selectors
20Sierpinski triangle Scheme final version
(define (sierpinski triangle) (cond
((too-small? triangle) t) (else (let
((a (a-point triangle)) (b (b-point
triangle)) (c (c-point triangle)))
(let ((a-b (mid-point a b)) (b-c
(mid-point b c)) (c-a (mid-point c
a))) (and (draw-triangle triangle)
(sierpinski )
(sierpinski )
(sierpinski )))))))
(make-triangle a a-b c-a)) (make-triangle b a-b
b-c)) (make-triangle c c-a b-c))
21Abstraction barriers
Triangles in problem domain
Triangles as lists of three points
Points as lists of two coordinates (x,y)
Points as lists
22Huffman encoding trees
23Data Transmission
sos
Bob
Alice
We wish to send information efficiently from
Alice to Bob
Morse code not necessarily the most efficient
you could think of
24Fixed Length Codes
Represent data as a sequence of 0s and
1s Sequence BACADAEAFABBAAAGAH
A fixed length code (ASCII)
A 000 B 001 C 010 D 011 E 100 F
101 G 110 H 111
Encoding of sequence
00100001000001100010000010100000100100000000011000
0111
The Encoding is 18x354 bits long. Can we make
the encoding shorter?
25Variable Length Code
Make use of frequencies. Frequency of A8, B3,
others 1.
A 0 B 100 C 1010 D 1011 E 1100
F 1101 G 1110 H 1111
Example BACADAEAFABBAAAGAH 100010100101101100011
010100100000111001111
But how do we decode?
26Prefix code ? Binary tree
Prefix code No codeword is a prefix of any other
codeword
A 0 B 100 C 1010 D 1011 E 1100
F 1101 G 1110 H 1111
27Decoding Example
10001010
10001010 B
10001010 BA
10001010 BAC
28Abstract rep. of code trees
Constructors make-leaf - Construct a
leaf make-code-tree - Construct a code
tree Predicates leaf? - Is
leaf? Selectors left-branch - Select left
branch right-branch - Select right
branch symbol-leaf - the symbol attached to
leaf
29Decoding a Message
(define (decode bits tree) (define (decode-one
bits current-branch) (if (null? bits)
'() (let ((next-branch
(choose-branch (car bits) current-branch)))
(if (leaf? next-branch) (cons
(symbol-leaf next-branch)
(decode-one (cdr bits) tree))
(decode-one (cdr bits) next-branch)))))
(decode-one bits tree))
(define (choose-branch bit branch) (cond ((
bit 0) (left-branch branch)) (( bit 1)
(right-branch branch)) (else (error "bad
bit -- CHOOSE-BRANCH" bit))))
30Huffman Tree Opt. Length Code
Optimal no other coding has better weighted
average length
31Representation
A,B,C,D,E,F,G,H
17
A
8
9
B,C,D,E,F,G,H
4
5
B,C,D
E,F,G,H
2
2
2
B
3
E,F
C,D
G,H
C
D
E
F
G
H
1
1
1
1
1
1
32Representation (Cont.)
(define (make-leaf symbol weight) (list 'leaf
symbol weight)) (define (leaf? object) (eq?
(car object) 'leaf)) (define (symbol-leaf x)
(cadr x)) (define (weight-leaf x) (caddr x))
33Representation (Cont.)
(define (make-code-tree left right) (list left
right (append (symbols left)
(symbols right)) ( (weight left) (weight
right)))) (define (left-branch tree) (car
tree)) (define (right-branch tree) (cadr tree))
34Representation (Cont.)
(define (symbols tree) (if (leaf? tree)
(list (symbol-leaf tree)) (caddr
tree))) (define (weight tree) (if (leaf?
tree) (weight-leaf tree) (cadddr
tree)))
35Huffmans Algorithm
Build tree bottom-up, so that lowest weight
leaves are farthest from the root.
Repeatedly Find two trees of lowest
weight. merge them to form a new tree whose
weight is the sum of their weights.
36Construction of Huffman Tree
37Construction of Huffman Tree
Initial leaves (A 8) (B 3) (C 1) (D 1) (E
1) (F 1) (G 1) (H 1) Merge (A
8) (B 3) (C D 2) (E 1) (F 1) (G 1) (H 1)
Merge (A 8) (B 3) (C D 2) (E F 2)
(G 1) (H 1) Merge (A 8) (B 3)
(C D 2) (E F 2) (G H 2) Merge
(A 8) (B 3) (C D 2) (E F G H 4)
Merge (A 8) (B C D 5) (E F G H
4) Merge (A 8) (B C D E F G H
9) Final merge (A B C D E F G H 17)
38Construction of Huffman Tree
(generate-huffman-tree '((A 8) (B 3) (C 1) (D 1)
(E 1) (F 1) (H 1) (G
1)) ((leaf a 8) ((((leaf g 1) (leaf h 1) (g h)
2) ((leaf f 1) (leaf e 1) (f e) 2) (g h f e) 4)
(((leaf d 1) (leaf c 1) (d c) 2) (leaf b 3) (d c
b) 5) (g h f e d c b) 9) (a g h f e d c b) 17)
39Construction of Huffman Tree
(define (generate-huffman-tree pairs)
(successive-merge (make-leaf-set pairs)))
(define (make-leaf-set pairs) (if (null?
pairs) '() (let ((pair (car pairs)))
(adjoin-set (make-leaf (car pair)
(cadr pair))
(make-leaf-set (cdr pairs))))))
Set of leaves, represented as lists ordered by
weight.
40Construction of Huffman Tree
Need a variation of the ordered adjoin-set (since
was written only for sets of numbers)
(define (adjoin-set x set) (cond ((null? set)
(list x)) ((lt (weight x) (weight (car
set)))(cons x set)) (else (cons (car
set) (adjoin-set x (cdr
set))))))
(define (successive-merge trees) (if (null? (cdr
trees)) (car trees) (let ((smallest
(car trees)) (2smallest (cadr trees))
(rest (cddr trees))) (successive-merge
(adjoin-set (make-code-tree
smallest 2smallest) rest)))))
41Printing List Structures
List notation (1 2 3)
Dot notation (1 . (2 . (3 . ())))
42Simple Dot Notation
(define (print-list-structure x) (define
(print-contents x) (print-list-structure (car
x)) (display " . ") (print-list-structure
(cdr x))) (cond ((null? x) (display "()"))
((atom? x) (display x)) (else (display
"(") (print-contents x)
(display ")"))))
43Pairs
Dot notation (1 . 2)
List notation Not every pair is a
list!!!!!
List notation (1)
Dot notation (1 . ())
44Print List Notation
(define (print-list-structure x) (define
(print-contents x) (print-list-structure (car
x)) (cond ((null? (cdr x)) nil)
((atom? (cdr x)) (display " . ")
(print-list-structure (cdr x)))
(else (display " ")
(print-contents (cdr x))))) (cond ((null? x)
(display "()")) ((atom? x) (display x))
(else (display "(")
(print-contents x) (display ")"))))
45Some examples
How do we create the following output ?
( 1 2 . 3)
(1 (2.3) 4.5)
(1 . 2 3)
Cannot!
Every pairs structure fits a unique list/dot
representation, but not every list/dot
representation has a matching structure