1 / 32

missing slides from lecture #8

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

Thomas
Télécharger la présentation

missing slides from lecture #8

An Image/Link below is provided (as is) to download presentation 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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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)) ==> (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)) ==> (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 -> 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) <handle product expression>) (else (error "unknown expression type" expr)) )) מבוא מורחב

  11. 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))) מבוא מורחב

  12. 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 מבוא מורחב

  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))) ==> (+ 10 (+ 2 x)) 4 (deriv’(+ (* x 3) 10 (+ 2 x)) ’x) ==> מבוא מורחב

  14. 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) מבוא מורחב

  15. Representing sets מבוא מורחב

  16. 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) מבוא מורחב

  17. 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)) מבוא מורחב

  18. Version 1: Represent a set as a list (define (adjoin-set x set) (if (element-of-set? x set) set (cons x set))) מבוא מורחב

  19. 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)))) מבוא מורחב

  20. 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))))) מבוא מורחב

  21. Complexity Element-of-set Adjoin-set Intersection-set Union-set (n) (n) (n2) (n2) מבוא מורחב

  22. 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) Adjoin-set is similar, please try by yourself מבוא מורחב

  23. 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 ? מבוא מורחב

  24. 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))))))) מבוא מורחב

  25. 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 מבוא מורחב

  26. unordered Element-of-set Adjoin-set Intersection-set Union-set (n) (n) (n2) (n2) Complexity ordered (n) (n) (n) (n) מבוא מורחב

  27. 3 7 7 1 9 3 5 9 12 5 1 12 Representing sets as binary trees מבוא מורחב

  28. 7 9 3 12 5 1 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)) מבוא מורחב

  29. 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) מבוא מורחב

  30. 1 7 3 9 3 5 12 5 1 7 9 12 Representing sets as binary trees (Cont.) מבוא מורחב

  31. Representing sets as binary trees (Cont.) (define (adjoin-set x set) (cond ((null? set) (make-tree x '() '())) ((= x (entry set)) set) ((< x (entry set)) (make-tree (entry set) (adjoin-set x (left-branch set)) (right-branch set))) ((> x (entry set)) (make-tree (entry set) (left-branch set) (adjoin-set x (right-branch set)))))) מבוא מורחב

  32. unordered ordered Element-of-set Adjoin-set Intersection-set Union-set (n) (n) (n) (n) (n) (n2) (n) (n2) Complexity trees (log(n)) (log(n)) (nlog(n)) (nlog(n)) מבוא מורחב

More Related