Title: missing slides from lecture
1missing slides fromlecture 8
2How does the interpreter prints lists and pairs ??
3First version, using 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 ")"))))
(p-l-s (list 1 2 3)) gt (1 . (2 . (3 . ())))
4Second version, try to identify lists
(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 ")"))))
(p-l-s (list 1 2 3)) gt (1 2 3)
5More examples
How to create the following output ?
( 1 2 . 3)
(cons 1 (cons 2 3))
(1. 2 3)
cannot
6Lecture 10
7A bigger example symbolic diffrentiation
83. A better implementation
- 1. Use cond instead of nested if expressions
- 2. Use data abstraction
- To use cond
- write a predicate that collects all tests to get
to a branch(define sum-expr? (lambda (e)
(and (pair? e) (eq? (car e) ')))) type Expr
-gt boolean - do this for every branch(define variable?
(lambda (e) (and (not (pair? e)) (symbol?
e))))
9Use data abstractions
- To eliminate dependence on the representation
- (define make-sum (lambda (e1 e2) (list ' e1
e2))(define addend (lambda (sum) (cadr sum)))
10A better implementation
- (define deriv (lambda (expr var)
- (cond
- ((number? expr) 0)
- ((variable? expr) (if (eq? expr var) 1 0))
- ((sum-expr? expr)
- (make-sum (deriv (addend expr) var)
- (deriv (augend expr) var)))
- ((product-expr? expr)
- lthandle product expressiongt)
- (else
- (error "unknown expression type" expr))
- ))
11Deriv Reduction problem
(deriv '( x 3) 'x) gt ( 1 0) (deriv '( x
y) 'x) gt ( ( x 0) ( 1 y)) (deriv '( ( x
y) ( x 3)) 'x) gt ( ( ( x y) ( 1 0)) (
( ( x 0) ( 1 y)) ( x 3)))
12Changes are isolated
- (deriv '( x y) 'x) gt ( 1 0) (a list!)
- (define (make-sum a1 a2)
- (cond ((number? a1 0) a2)
- ((number? a2 0) a1)
- ((and (number? a1) (number? a2))
- ( a1 a2))
- (else (list ' a1 a2))))
- (define (number? exp num)
- (and (number? exp) ( exp num)))
- (deriv '( x y) 'x) gt 1
- (deriv '( x y) 'x) gt y
-
13Variable number of arguments in sums and products
(deriv ( ( x 3) 10 ( 2 x)) x)
(define (augend s) (if (null? (cdddr s))
(caddr s) (cons ' (cddr s))))
(augend ( ( x 3) 10 ( 2 x))) gt ( 10 ( 2
x))
4
(deriv ( ( x 3) 10 ( 2 x)) x) gt
14Variable number of summands and multipliers
(deriv '( ( x a) ( x b) ( x c)) 'x) gt ( a
( b c))
(define (make-sum a1 a2) (cond ((number? a1 0)
a2) ((number? a2 0) a1) ((and
(number? a1) (number? a2)) ( a1 a2))
((sum? a2) (cons ' (cons a1 (cdr a2))))
(else (list ' a1 a2))))
(deriv '( ( x a) ( x b) ( x c)) 'x) gt ( a b
c)
15Adding exponential expressions
(fn) nf(n-1)f
(define (deriv exp var) (cond
((exponential exp) (make-product
(make-product (exponent exp)
(make-exponential
(base exp)
(- (exponent exp) 1))) (deriv
(base exp) var)))))
16Constructors and selectors for exponentiation
(define (make-exponential b e) (cond (( e 0)
1) (( e 1) b) (else (list
' b e))))
(define (exponential? exp) (and (pair? exp)
(eq? (car exp) '))) (define (base exp) (cadr
exp)) (define (exponent exp) (caddr exp))
17Representing sets
18Definitions
A set is a collection of distinct
items (element-of-set? x set) (adjoin-set x
set) (union-set s1 s2) (intersection-set s1 s2)
19Version 1 Represent a set as an unordered list
(define (element-of-set? x set) (cond ((null?
set) false) ((equal? x (car set)) true)
(else (element-of-set? x (cdr set)))))
equal? Like eq? for symbols. Works for numbers
Works recursively for compounds
Apply equal? to the
components. (eq? (list a b) (list a
b)) (equal? (list a b) (list a b))
20Version 1 Represent a set as a list
(define (adjoin-set x set) (if (element-of-set?
x set) set (cons x set)))
21Version 1 Represent a set as a list
(define (intersection-set set1 set2) (cond ((or
(null? set1) (null? set2)) '())
((element-of-set? (car set1) set2) (cons
(car set1) (intersection-set (cdr
set1) set2))) (else (intersection-set
(cdr set1) set2))))
22Version 1 Represent a set as a list
(define (union-set set1 set2) (cond ((null?
set1) set2)) ((not (element-of-set? (car
set1) set2)) (cons (car set1)
(union-set (cdr set1) set2))) (else
(union-set (cdr set1) set2))))
(define (union-set set1 set2) (cond ((null?
set1) set2)) (else (adjoin-set (car
set1) (union-set (cdr set1)
set2)))))
23Complexity
Element-of-set Adjoin-set Intersection-set
Union-set
?(n)
?(n)
?(n2)
?(n2)
24Version 2 Representing a set as an ordered list
(define (element-of-set? x set) (cond ((null?
set) false) (( x (car set)) true)
((lt x (car set)) false) (else
(element-of-set? x (cdr set)))))
n/2 steps on average ? ?(n)
Adjoin-set is similar, please try by yourself
25Ordered lists (cont.)
(define (intersection-set set1 set2) (cond ((or
(null? set1) (null? set2)) '())
((element-of-set? (car set1) set2) (cons
(car set1) (intersection-set (cdr
set1) set2))) (else (intersection-set
(cdr set1) set2))))
Can we do it better ?
26Ordered lists (cont.)
(define (intersection-set set1 set2) (if (or
(null? set1) (null? set2)) '() (let
((x1 (car set1)) (x2 (car set2))) (cond
(( x1 x2) (cons x1
(intersection-set (cdr set1)
(cdr set2))))
((lt x1 x2) (intersection-set
(cdr set1) set2)) ((lt x2 x1)
(intersection-set set1 (cdr set2)))))))
27Ordered lists (Cont.)
set1 set2 intersection (1 3 7
9) (1 4 6 7) (1 (3 7 9) (4 6 7)
(1 (7 9) (4 6 7) (1 (7 9) (6
7) (1 (7 9) (7)
(1 (9) () (1 7)
Time and space ? ?(n) Union -- similar
28Complexity
ordered
?(n)
?(n)
?(n)
?(n)
29Representing sets as binary trees
30Representing sets as binary trees (Cont.)
(define (entry tree) (car tree)) (define
(left-branch tree) (cadr tree)) (define
(right-branch tree) (caddr tree)) (define
(make-tree entry left right) (list entry left
right))
31Representing sets as binary trees (Cont.)
(define (element-of-set? x set) (cond ((null?
set) false) (( x (entry set)) true)
((lt x (entry set)) (element-of-set? x
(left-branch set))) ((gt x (entry set))
(element-of-set? x (right-branch set)))))
Complexity ?(d) If tree is balanced d ? log(n)
32Representing sets as binary trees (Cont.)
33Representing sets as binary trees (Cont.)
(define (adjoin-set x set) (cond ((null? set)
(make-tree x '() '())) (( x (entry set))
set) ((lt x (entry set))
(make-tree (entry set)
(adjoin-set x (left-branch set))
(right-branch set))) ((gt x (entry
set)) (make-tree (entry set)
(left-branch set)
(adjoin-set x (right-branch set))))))
34Complexity
trees
?(log(n))
?(log(n))
?(nlog(n))
?(nlog(n))