missing slides from lecture #8

1 / 32

# missing slides from lecture #8 - PowerPoint PPT Presentation

missing slides from lecture #8. How does the interpreter prints lists and pairs ??. 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)))

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## missing slides from lecture #8

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

מבוא מורחב

### How does the interpreter prints lists and pairs ??

מבוא מורחב

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)) ==> (1 . (2 . (3 . ())))

מבוא מורחב

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)) ==> (1 2 3)

More examples

How to create the following output ?

( 1 2 . 3)

(cons 1 (cons 2 3))

(1. 2 3)

cannot

מבוא מורחב

מבוא מורחב

### A bigger example: symbolic diffrentiation

מבוא מורחב

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 -> boolean
• do this for every branch:(define variable? (lambda (e) (and (not (pair? e)) (symbol? e))))

מבוא מורחב

Use data abstractions
• To eliminate dependence on the representation:

(define make-sum (lambda (e1 e2) (list '+ e1 e2))(define addend (lambda (sum) (cadr sum)))

מבוא מורחב

A better implementation

(define deriv (lambda (expr var)

(cond

((number? expr) 0)

((variable? expr) (if (eq? expr var) 1 0))

((sum-expr? expr)

(deriv (augend expr) var)))

((product-expr? expr)

<handle product expression>)

(else

(error "unknown expression type" expr))

))

מבוא מורחב

Deriv: Reduction problem

(deriv '(+ x 3) 'x) ==>

(+ 1 0)

(deriv '(* x y) 'x) ==>

(+ (* x 0) (* 1 y))

(deriv '(* (* x y) (+ x 3)) 'x) ==>

(+ (* (* x y) (+ 1 0)) (* (+ (* x 0) (* 1 y)) (+ x 3)))

מבוא מורחב

Changes are isolated

(deriv '(+ x y) 'x) ==> (+ 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) ==> 1

(deriv '(* x y) 'x) ==> y

מבוא מורחב

Variable number of arguments in sums and products

(deriv’(+ (* x 3) 10 (+ 2 x)) ’x)

(define (augend s)

(if (null? (cdddr s))

(cons '+ (cddr s))))

(augend ’(+ (* x 3) 10 (+ 2 x))) ==> (+ 10 (+ 2 x))

4

(deriv’(+ (* x 3) 10 (+ 2 x)) ’x) ==>

מבוא מורחב

Variable number of summands and multipliers

(deriv '(+ (* x a) (* x b) (* x c)) 'x) =>

(+ 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) =>

(+ a b c)

מבוא מורחב

### Representing sets

מבוא מורחב

Definitions

A set is a collection of distinct items

(element-of-set? x set)

(union-set s1 s2)

(intersection-set s1 s2)

מבוא מורחב

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))

מבוא מורחב

Version 1: Represent a set as a list

(if (element-of-set? x set)

set

(cons x set)))

מבוא מורחב

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))))

מבוא מורחב

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))

(union-set (cdr set1) set2)))))

מבוא מורחב

Complexity

Element-of-set

Intersection-set

Union-set

(n)

(n)

(n2)

(n2)

מבוא מורחב

Version 2: Representing a set as an ordered list

(define (element-of-set? x set)

(cond ((null? set) false)

((= x (car set)) true)

((< x (car set)) false)

(else (element-of-set? x (cdr set)))))

n/2 steps on average  (n)

מבוא מורחב

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 ?

מבוא מורחב

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))))

((< x1 x2)

(intersection-set (cdr set1) set2))

((< x2 x1)

(intersection-set set1 (cdr set2)))))))

מבוא מורחב

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

מבוא מורחב

unordered

Element-of-set

Intersection-set

Union-set

(n)

(n)

(n2)

(n2)

Complexity

ordered

(n)

(n)

(n)

(n)

מבוא מורחב

3

7

7

1

9

3

5

9

12

5

1

12

Representing sets as binary trees

מבוא מורחב

7

9

3

12

5

1

Representing sets as binary trees (Cont.)

(define (entry tree) (car tree))

(define (make-tree entry left right)

(list entry left right))

מבוא מורחב

Representing sets as binary trees (Cont.)

(define (element-of-set? x set)

(cond ((null? set) false)

((= x (entry set)) true)

((< x (entry set))

(element-of-set? x (left-branch set)))

((> x (entry set))

(element-of-set? x (right-branch set)))))

Complexity: (d)

If tree is balanced d  log(n)

מבוא מורחב

1

7

3

9

3

5

12

5

1

7

9

12

Representing sets as binary trees (Cont.)

מבוא מורחב

Representing sets as binary trees (Cont.)

(cond ((null? set) (make-tree x '() '()))

((= x (entry set)) set)

((< x (entry set))

(make-tree (entry set)

(right-branch set)))

((> x (entry set))

(make-tree (entry set)

(left-branch set)

מבוא מורחב

unordered

ordered

Element-of-set

Intersection-set

Union-set

(n)

(n)

(n)

(n)

(n)

(n2)

(n)

(n2)

Complexity

trees

(log(n))

(log(n))

(nlog(n))

(nlog(n))

מבוא מורחב