1 / 30

Lecture #8

Lecture #8. Patterns. Enumerating Mapping Filtering Accumulating. (integers-between 2 4) (cons 2 (integers-between 3 4))) (cons 2 (cons 3 (integers-between 4 4))) (cons 2 (cons 3 (cons 4 (integers-between 5 4)))) (cons 2 (cons 3 (cons 4 nil))) (list 2 3 4). 2. 3. 4. Enumerating.

lradford
Download Presentation

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. Lecture #8 מבוא מורחב

  2. Patterns • Enumerating • Mapping • Filtering • Accumulating מבוא מורחב

  3. (integers-between 2 4) (cons 2 (integers-between 3 4))) (cons 2 (cons 3 (integers-between 4 4))) (cons 2 (cons 3 (cons 4 (integers-between 5 4)))) (cons 2 (cons 3 (cons 4 nil))) (list 2 3 4) 2 3 4 Enumerating (define (integers-between hi lo) (cond ((> lo hi) nil) (else (cons lo (integers-between (+ 1 lo) hi))))) מבוא מורחב

  4. 6 children or subtrees root 4 2 2 6 8 4 8 Trees We can view a list of possibly other lists and atoms as a tree. (define tree (list 2 (list 6 8) 4)) (length tree)  3 מבוא מורחב

  5. Enumerate-Leaves • Goal: given a tree, produce a list of all the leaves • Strategy • base case: list of empty tree is empty list • base case: list of a leaf is one element list • otherwise, recursive strategy: build a new list from a list of the leaves of the first child and a list of the leaves of the rest of the children מבוא מורחב

  6. Enumerate-leaves (define (enumerate-leaves tree) (cond ((null? tree) nil) ;base case ((leaf? tree) ) ;base case (else ;recursive case ( (enumerate-leaves (car tree)) (enumerate-leaves (cdr tree)))))) מבוא מורחב

  7. 5 7 4 2 4 2 5 7 Enumerate-Leaves (define (enumerate-leaves tree) (cond ((null? tree) nil) ;base case ((leaf? tree) (list tree)) ;base case (else ;recursive case (append(enumerate-leaves (car tree)) (enumerate-leaves (cdr tree)))))) מבוא מורחב

  8. Append 5 7 2 ap (el 4) (el ((5 7) 2) ) ap (cl (5 7)) (el (2)) ap (el (7)) (el 5) (el 2) (el nil) ap (el 7) (el nil) Enumerate-leaves (el (4 (5 7) 2)) (4 5 7 2) (5 7 2) (4) (5 7) (2) ap (7) (5) (2) () (7) ()

  9. countleaves • Strategy • base case: count of an empty tree is 0 • base case: count of a leaf is 1 • recursive strategy: the count of a tree is the sum of the countleaves of each child in the tree. (define (countleaves tree) (cond ((null? tree) 0) ;base case ((leaf? tree) 1) ;base case (else ;recursive case (+ (countleaves (car tree)) (countleaves (cdr tree)))))) (define (leaf? x) (not (pair? x))) מבוא מורחב

  10. 5 my-tree 4 (cl 4) (cl ((5 7) 2) ) + + (cl (5 7)) (cl (2)) + + (cl (7)) (cl 2) (cl 5) (cl nil) + 7 2 (cl 7) (cl nil) countleaves – example (define my-tree (list 4 (list 5 7) 2)) (countleaves my-tree) (cl (4 (5 7) 2)) 1 1 1 0 1 0

  11. Your Turn: scale-tree • Goal: given a tree, produce a new tree with all the leaves scaled • Strategy • base case: scale of empty tree is empty tree • base case: scale of a leaf is product • otherwise, recursive strategy: build a new tree from a scaled version of the first child and a scaled version of the rest of children מבוא מורחב

  12. Scale-tree (define (scale-tree tree factor) (cond ((null? tree) nil) ;base case ((leaf? tree) (* tree factor)) (else ;recursive case (cons (scale-tree (cdr tree) factor ))))) (scale-tree (car tree) factor) מבוא מורחב

  13. Alternative scale-tree • Strategy • base case: scale of empty tree is empty tree • base case: scale of a leaf is product • otherwise: a tree is a list of subtrees and use map. (define (scale-tree tree factor) (cond ((null? tree) nil) ((leaf? tree) (* tree factor)) (else ;it’s a list of subtrees (map (lambda (child) (scale-tree child factor)) tree))))

  14. Mapping (define (map proc lst) (if (null? lst) nil (cons (proc (car lst)) (map proc (cdr lst))))) (define (square-list lst) (map square lst)) (define (scale-list lst c) (map (lambda (x) (* c x)) lst)) (scale-list (integers-between 1 5) 10) ==> (10 20 30 40 50) מבוא מורחב

  15. (define (filter pred lst) (cond ((null? lst) nil) ((pred (car lst)) (cons (car lst) (filter pred (cdr lst)))) (else (filter pred (cdr lst))))) Filtering (filter odd? (square-list (integers-between 1 10))) (1 9 25 49 81) מבוא מורחב

  16. Accumulating (define (add-up lst) (if (null? lst) 0 (+ (car lst) (add-up (cdr lst))))) (define (mult-all lst) (if (null? lst) 1 (* (car lst) (mult-all (cdr lst))))) (define (accumulate op init lst) (if (null? lst) init (op (car lst) (accumulate op init (cdr lst))))) מבוא מורחב

  17. eln eln-1 init el1 …….. op op op ... op Accumulating (cont.) (define (accumulate op init lst) (if (null? lst) init (op (car lst) (accumulate op init (cdr lst))))) (define (add-up lst) (accumulate + 0 lst)) מבוא מורחב

  18. eln eln eln-1 eln-1 lst2 0 el1 el1 …….. …….. +1 +1 +1 ... ... +1 Length and append as accumulation (define (length lst) (accumulate (lambda (x y) (+ 1 y)) 0 lst)) (define (append lst1 lst2) (accumulate cons lst2 lst1) מבוא מורחב

  19. More examples (define (fib n) (cond ((= n 0) 0) ((= n 1) 1) (else (+ (fib (- n 1)) (fib (- n 2)))))) (map fib (integers-between 10 20)) ==> (55 89 … 6765) מבוא מורחב

  20. Multiply all fibonacci values for even integers between hi and lo Using our tools: (define (easy lo hi) (accumulate * 1 (map fib (filter even? (integers-between lo hi))))) Without: (define (hard lo hi) (cond ((> lo hi) 1) ((even? lo) (* (fib lo) (hard (+ lo 1) hi))) (else (hard (+ lo 1) hi)))) מבוא מורחב

  21. Even-fibs • Enumerates the integers from 0 to n • Computes the Fibonacci number for each integer • Filters them selecting the even ones • Accumulates the results using cons • Sum-odd-squares • Enumerates the leaves of a tree • Filters them, selecting the odd ones • Squares each of the selected ones • Accumulates the results using +

  22. tree 0 + square odd? enumerate leaves accumulate map filter 0, n cons nil even? fib integers between accumulate filter map even-fibs sum-odd-squares in a tree Flow along horizontal arrows. מבוא מורחב

  23. 0, n cons nil even? fib integers between accumulate filter map Implementation (define (even-fibs n) (define (next k) (if (< n k) nil (let ((f (fib k))) (if (even? f) (cons f (next (+ k 1))) (next (+ k 1)))))) (next 0)) (define (even-fibs n) (accumulate cons nil (filter even? (map fib (integers-between 0 n)))))

  24. tree 0 + square odd? enumerate leaves accumulate map filter Implementation (define (sum-odd-squares tree) (accumulate + 0 (map square (filter odd? (enumerate-leaves tree))))) (define (sum-odd-squares tree) (cond ((null? tree) 0) ((leaf? tree) (if (odd? tree) (square tree) 0)) (else (+ (sum-odd-squares (car tree)) (sum-odd-squares (cdr tree))))))

  25. How does the interpreter prints lists and pairs ?? מבוא מורחב

  26. 2 1 3 1 3 List notation (1 (5 7) 3) 5 7 Dot notation (1 . ((5 . (7 . ())) . (3 . ())) Printing List Structures List notation (1 2 3) Dot notation (1 . (2 . (3 . ()))) מבוא מורחב

  27. Simple 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 ")")))) (define (atom? z) (and (not (pair? z)) (not (null? z)))) מבוא מורחב

  28. 1 2 1 Pairs Dot notation (1 . 2) List notation Not every pair is a list!!!!! List notation (1) Dot notation (1 . ()) מבוא מורחב

  29. Print 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 ")"))))

  30. 2 3 (cons 1 (cons 2 3)) 1 More examples How to create the following output ? ( 1 2 . 3) (1 . 2 3) cannot מבוא מורחב

More Related