missing slides from lecture - PowerPoint PPT Presentation

About This Presentation
Title:

missing slides from lecture

Description:

(deriv (augend expr) var))) ((product-expr? expr) handle product expression ) (else ... (define (augend s) (if (null? ( cdddr s)) (caddr s) (cons ' (cddr s) ... – PowerPoint PPT presentation

Number of Views:138
Avg rating:3.0/5.0
Slides: 33
Provided by: johnc92
Category:

less

Transcript and Presenter's Notes

Title: missing slides from lecture


1
missing slides fromlecture 8
2
How does the interpreter prints lists and pairs ??
3
First 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 . ())))
4
Second 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)
5
More examples
How to create the following output ?
( 1 2 . 3)
(cons 1 (cons 2 3))
(1. 2 3)
cannot
6
Lecture 10
7
A bigger example symbolic diffrentiation
8
3. 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))))

9
Use data abstractions
  • To eliminate dependence on the representation
  • (define make-sum (lambda (e1 e2) (list ' e1
    e2))(define addend (lambda (sum) (cadr sum)))

10
A 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))
  • ))

11
Deriv 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)))
12
Changes 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

13
Variable 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
14
Variable 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)
15
Adding 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)))))
16
Constructors 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))

17
Representing sets
18
Definitions
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)
19
Version 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))
20
Version 1 Represent a set as a list
(define (adjoin-set x set) (if (element-of-set?
x set) set (cons x set)))
21
Version 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))))
22
Version 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)))))
23
Complexity
Element-of-set Adjoin-set Intersection-set
Union-set
?(n)
?(n)
?(n2)
?(n2)
24
Version 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
25
Ordered 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 ?
26
Ordered 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)))))))
27
Ordered 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
28
Complexity
ordered
?(n)
?(n)
?(n)
?(n)
29
Representing sets as binary trees
30
Representing 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))
31
Representing 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)
32
Representing sets as binary trees (Cont.)
33
Representing 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))))))
34
Complexity
trees
?(log(n))
?(log(n))
?(nlog(n))
?(nlog(n))
Write a Comment
User Comments (0)
About PowerShow.com