מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).

Slides:



Advertisements
Similar presentations
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append.
Advertisements

1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
מבוא מורחב 1 Lecture #7. מבוא מורחב 2 The rational number abstraction Wishful thinking: (make-rat ) Creates a rational number (numer ) Returns the numerator.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. Outline Let* List and pairs manipulations –Insertion Sort Abstraction Barriers –Fractals –Mobile 2.
מבוא מורחב - שיעור 91 Lecture 9 Lists continued: Map, Filter, Accumulate, Lists as interfaces.
6.001 SICP SICP – October Trees Trevor Darrell 32-D512 Office Hour: W web page:
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 7 1. Outline More list examples Symbols 2.
מבוא מורחב - שיעור 15 1 Lecture 15 Streams. מבוא מורחב - שיעור 15 2 Streams: Motivation (define (sum-primes a b) (define (iter count accum) (cond ((>
6.001 SICP SICP – October HOPs, Lists, Trees Trevor Darrell 32-D512 Office Hour: W web page:
מבוא מורחב - שיעור 10 1 Symbols Manipulating lists and trees of symbols: symbolic differentiation Lecture 10.
Data Abstraction… The truth comes out…. What we’re doing today… Abstraction ADT: Dotted Pair ADT: List Box and Pointer List Recursion Deep List Recursion.
6.001 SICP SICP – September ? 6001-Introduction Trevor Darrell 32-D web page: section.
6.001 SICP SICP – October Introduction Trevor Darrell 32-D512 Office Hour: W web page:
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 13. Streams 3.5, pages definitions file on web 2.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. Outline Abstraction Barriers –Fractals –Mobile List and pairs manipulations –Insertion Sort 2.
מבוא מורחב - שיעור 12 1 Lecture 12 Data directed programming Message passing dotted-tail notation & apply Section 2.4, pages ,2.5.2 pages.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
מבוא מורחב 1 Lecture #8. מבוא מורחב 2 Shall we have some real fun.. Lets write a procedure scramble.. (scramble (list )) ==> ( ) (scramble.
Quiz: Box and Pointer fun! (cons (cons (cons ‘hey (cons ‘there nil)) nil) (cons ‘wow nil)) (list ‘boo (append (list ‘hoo ‘hoo) (cons ‘see ‘me)))
PPL Pairs, lists and data abstraction. Data Abstraction? An interface: separate implementation from usage Think of the Map interface in Java: we know.
Spring 2008Programming Development Techniques 1 Topic 6 Hierarchical Data and the Closure Property Section September 2008.
1 Append: process  (append list1 list2) (cons 1 (append ‘(2) list2)) (cons 1 (cons 2 (append ‘() list2))) (cons 1 (cons 2 list2)) (define (append list1.
Practice session #6: 1. Sequence operations 2. Partial evaluation with currying 3. Lazy-lists.
Today’s topic: Abstraction Compound Data Data Abstractions: Isolate use of data abstraction from details of implementation Relationship between data abstraction.
מבוא מורחב 1 Lecture #9. מבוא מורחב 2 Symbol: a primitive type constructors: (quote alpha) ==> quote is a special form. One argument: a name. selectors.
Abstraction: Procedures as Parameters CMSC Introduction to Computer Programming October 14, 2002.
Fall 2008Programming Development Techniques 1 Topic 8 Sequences as Conventional Interfaces Section October 2008.
Scope: What’s in a Name? CMSC Introduction to Computer Programming October 16, 2002.
1 Data Abstraction. Pairs and Lists. (SICP Sections – 2.2.1)
PPL CPS. Moed A 2007 Solution (define scale-tree (λ (tree factor) (map (λ (sub-tree) (if (list? sub-tree) (scale-tree sub-tree factor) (* sub-tree.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 8. Outline 1.The special form quote 2.Data abstraction: Trie 3.Alternative list: Triplets 4.Accumulate-n.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 6. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append.
PPL CPS. Moed A 2007 Solution (define scale-tree (λ (tree factor) (map (λ (sub-tree) (if (list? sub-tree) (scale-tree sub-tree factor) (* sub-tree.
מבוא מורחב שיעור 7 1 Lecture 7 Data Abstraction. Pairs and Lists. (Sections – 2.2.1)
1 Recursive Data Structures CS 270 Math Foundations of CS Jeremy Johnson.
Lecture #5 מבוא מורחב.
Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16.
Representing Sets (2.3.3) Huffman Encoding Trees (2.3.4)
6.001 SICP Variations on a Scheme
Pairs and Lists. Data Abstraction. SICP: Sections – 2.2.1
PPL Lecture Notes: Chapter 3 High-Order Procedures Revisited.
PPL Lazy Lists.
(defmacro (delay x) (list 'lambda () x))
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
6.001 SICP Data abstractions
Streams Sections 3.5.1,3.5.2 Pages
Lecture #5 מבוא מורחב.
The Metacircular Evaluator
Lecture 18 Infinite Streams and
PPL Sequence Interface.
Lecture 18.
Lecture #8 מבוא מורחב.
6.001 SICP Streams – the lazy way
The Metacircular Evaluator (Continued)
Lecture #9 מבוא מורחב.
Lecture 13 - Assignment and the environments model Chapter 3
topics mutable data structures
6.001 SICP Data Mutation Primitive and Compound Data Mutators
6.001 SICP Data abstractions
Lecture #7 מבוא מורחב.
List and list operations (continue).
Today’s topics Abstractions Procedural Data
Lecture # , , , , מבוא מורחב.
Lisp.
list data list 만들기 list 사용하기 nil : list link :  * list -> list
Presentation transcript:

מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).

מבוא מורחב - שיעור 82 Formal Definition of a List A list is either –‘() -- The empty list –A pair whose cdr is a list. Note that lists are closed under the operations cons and cdr.

מבוא מורחב - שיעור 83 More Elaborate Lists (list ) (cons (list 1 2) (list 3 4)) (list (list 1 2) (list 3 4))

מבוא מורחב - שיעור 84 The Predicate Null? null? : anytype -> boolean (null? ) #t if evaluates to empty list #f otherwise (null? 2)  #f (null? (list 1))  #f (null? (cdr (list 1)))  #t (null? ’())  #t (null? null)  #t

מבוא מורחב - שיעור 85 The Predicate Pair? pair? : anytype -> boolean (pair? ) #t if evaluates to a pair #f otherwise. (pair? (cons 1 2))  #t (pair? (cons 1 (cons 1 2)))  #t (pair? (list 1))  #t (pair? ’())  #f (pair? 3)  #f (pair? pair?)  #f

מבוא מורחב - שיעור 86 The Predicate Atom? atom? : anytype -> boolean (define (atom? z) (and (not (pair? z)) (not (null? z)))) (define (square x) (* x x)) (atom? square)  #t (atom? 3)  #t (atom? (cons 1 2))  #f

מבוא מורחב - שיעור 87 More examples  (define digits (list )) ( )  (define digits1 (cons 0 digits))  digits1  (define l (list 0 digits))  l ? (0 ( ))

מבוא מורחב - שיעור 88 The procedure length  (define digits (list ))  (length digits) 9  (define l null)  (length l) 0  (define l (cons 1 l))  (length l) 1 (define (length l) (if (null? l) 0 (+ 1 (length (cdr l)))))

מבוא מורחב - שיעור 89 Primitives to handle lists (null? ) #t if evaluates to empty list #f otherwise (pair? ) #t if evaluates to a pair #f otherwise. (define (atom? z) (and (not (pair? z)) (not (null? z))))

מבוא מורחב - שיעור 810 Working with lists: some basic list manipulation

מבוא מורחב - שיעור 811 Cdr ing Down a List (define (list-ref lst n) (if (= n 0) (car lst) (list-ref (cdr lst) (- n 1)))) (list-ref (list 1 2 3) 0)  (list-ref (list 1 2 3) 3)  1 Error

מבוא מורחב - שיעור 812 Cdr ing Down a List, another example (define (length lst) (if (null? lst) 0 (+ 1 (length (cdr lst)))))

מבוא מורחב - שיעור 813 Cons ing Up a List  (define squares (list ))  (define odds (list ))  (append squares odds) ( )  (append odds squares) ( ) (define (append list1 list2) (cond ((null? list1) list2) ; base (else (cons (car list1) ; recursion (append (cdr list1) list2))))) list2 list1 Can’t make this pointer Change, so…

מבוא מורחב - שיעור 814 Append: process  (append list1 list2) (cons 1 (append (2) list2)) (cons 1 (cons 2 (append () list2))) (cons 1 (cons 2 list2)) (define (append list1 list2) (cond ((null? list1) list2) ; base (else (cons (car list1) ; recursion (append (cdr list1) list2))))) list2 list1 12 ( )  (define list1 (list 1 2))  (define list2 (list 3 4))

6/26/ Reverse of a list (reverse (list )) (define (reverse lst) (cond ((null? lst) lst) (else ( (reverse (cdr lst)) ))))) cons (car lst) (reverse (cdr lst)) Wishful thinking… (car lst) cons

6/26/ Reverse of a list (reverse (list )) (define (reverse lst) (cond ((null? lst) lst) (else ( (reverse (cdr lst)) ))))) append (list (car lst)) (append ) (reverse (cdr lst))(list (car lst)) Append: T(n) = c*n =  (n) Reverse: T(n) = c*(n-1) + c*(n-2)+ … + c*1 =  (n 2 )

מבוא מורחב - שיעור 817 Reverse (reverse (list 1 2 3)) (append (reverse (2 3)) (1)) (append (append (reverse (3)) (2)) (1)) (append (append (append (reverse ()) (3)) (2)) (1)) (append (append (append null (3)) (2)) (1)) (append (append (3) (2)) (1)) (append (3 2) (1)) (3 2 1) (define (reverse lst) (cond ((null? lst) lst) (else (append (reverse (cdr lst)) (list (car lst))))))) Append: T(n1) = c*n1 =  (n1) (n1 is length of list1) Reverse: T(n) = c*(n-1) + c*(n-2) … c*1 =  (n 2 )  (reverse (list 1 2 3)) (3 2 1)

מבוא מורחב - שיעור 818 Enumerating (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 null))) (2 3 4) (define (integers-between lo hi) (cond ((> lo hi) null) (else (cons lo (integers-between (+ 1 lo) hi))))) 234

מבוא מורחב - שיעור 819 Enumerate Squares (enumerate-squares 2 4) (cons 4 (enumerate-squares 3 4))) (cons 4 (cons 9 (enumerate-squares 4 4))) (cons 4 (cons 9 (cons 16 (enumerate-squares 5 4)))) (cons 4 (cons 9 (cons 16 ‘()))) (4 9 16) (define (enumerate-squares from to) (cond ((> from to) '()) (else (cons (square from) (enumerate-squares (+ 1 from) to)))))

מבוא מורחב - שיעור 820 Trees Abstract tree: a leaf (a node that has no children, and contains data) - is a tree an internal node (a node whose children are trees) – is a tree leaf internal node Implementation of tree: a leaf - will be the data itself an internal node – will be a list of its children

מבוא מורחב - שיעור 821 Count Leaves of a Tree 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. Implementation: (define (leaf? x) (atom? x))

מבוא מורחב - שיעור 822 Count Leaves (define (countleaves tree) (cond ((null? tree) 0) ;base case ((leaf? tree) 1) ;base case (else ;recursive case (+ (countleaves (car tree)) (countleaves (cdr tree)))))) (define my-tree (list 4 (list 5 7) 2)) 42 57

מבוא מורחב - שיעור 823 Countleaves (countleaves my-tree ) ==> 4 (cl (4 (5 7) 2)) + (cl 4)(cl ((5 7) 2) ) + (cl (5 7))(cl (2)) + (cl 2) (cl null) + (cl 5) (cl (7)) + (cl 7) (cl null) my-tree

מבוא מורחב - שיעור 824 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

מבוא מורחב - שיעור 825 Enumerate-Leaves (define (enumerate-leaves tree) (cond ((null? tree) null) ;base case ((leaf? tree) ) ;base case (else ;recursive case ( (enumerate-leaves (car tree)) (enumerate-leaves (cdr tree))))))

מבוא מורחב - שיעור 826 Enumerate-Leaves (define (enumerate-leaves tree) (cond ((null? tree) null) ;base case ((leaf? tree) (list tree)) ;base case (else ;recursive case (append (enumerate-leaves (car tree)) (enumerate-leaves (cdr tree))))))

מבוא מורחב - שיעור 827 Enumerate-leaves (el (4 (5 7) 2)) ap (el 4)(el ((5 7) 2) ) ap (cl (5 7))(el (2)) ap (el 2) (el nil) ap (el 5) (el (7)) ap (el 7) (el nil) (4) (5) (7)() (2) () (7) (5 7)(2) (5 7 2) ( )

מבוא מורחב - שיעור 828 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

מבוא מורחב - שיעור 829 Scale-tree (define (scale-tree tree factor) (cond ((null? tree) ) ;base case ((leaf? tree) ) (else ;recursive case (cons )))) (scale-tree (car tree) factor) null (* tree factor) (scale-tree (cdr tree) factor)

מבוא מורחב - שיעור 830 List abstraction Find common high order patterns Distill them into high order procedures Use these procedures to simplify list operations Mapping Filtering Accumulating Patterns:

מבוא מורחב - שיעור 831 Mapping (define (map proc lst) (if (null? lst) null (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) ==> ( )

מבוא מורחב - שיעור 832 Mapping: process (define (map proc lst) (if (null? lst) null (cons (proc (car lst)) (map proc (cdr lst))))) (map square (list 1 2 3)) (cons (square 1) (map square (list 2 3))) (cons 1 (map square (list 2 3))) (cons 1 (cons (square 2) (map square (list 3)))) (cons 1 (cons 4 (map square (list 3)))) (cons 1 (cons 4 (cons (square 3) (map square null)))) (cons 1 (cons 4 (cons 9 (map square null)))) (cons 1 (cons 4 (cons 9 null))) (1 4 9)

מבוא מורחב - שיעור 833 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) null) ((leaf? tree) (* tree factor)) (else ;it’s a list of subtrees (map (lambda (child) (scale-tree child factor)) tree))))

מבוא מורחב - שיעור 834 Generalized Mapping (map + (list 1 2 3) (list ) (list )) ==> ( ) (map (lambda (x y) (+ x (* 2 y))) (list 1 2 3) (list 4 5 6)) ==> ( ) (map … ) Returns a list in which proc is applied to the i-th elements of the lists respectively. We will see how to write such a procedure later!

מבוא מורחב - שיעור 835 Filtering (define (filter pred lst) (cond ((null? lst) null) ((pred (car lst)) (cons (car lst) (filter pred (cdr lst)))) (else (filter pred (cdr lst))))) (filter odd? (integers-between 1 10)) ( )

מבוא מורחב - שיעור 836 Filtering: process (define (filter pred lst) (cond ((null? lst) null) ((pred (car lst)) (cons (car lst) (filter pred (cdr lst)))) (else (filter pred (cdr lst))))) (filter odd? (list )) (cons 1 (filter odd? (list 2 3 4))) (cons 1 (filter odd? (list 3 4))) (cons 1 (cons 3 (filter odd? (list 4)))) (cons 1 (cons 3 (filter odd? null))) (cons 1 (cons 3 null)) (1 3)

מבוא מורחב - שיעור 837 Finding all the Primes: Sieve of Eratosthenes (a.k.a. Beta)

מבוא מורחב - שיעור And here’s how to do it! (define (sieve lst) (if (null? lst) ‘() (cons (car lst) (sieve (filter (lambda (x) (not (divisible? x (car lst)))) (cdr lst)))))) (cons 2 (sieve (filter (lambda (x) (not (divisible? X 2) (list …100 )))) ==> (sieve (list … 100))

מבוא מורחב - שיעור 839 How sieve works Sieve takes as argument a list of numbers L and returns a list M. Take x, the first element of L and make it the first element in M. Drop all numbers divisible by x from (cdr L). Call sieve on the resulting list, to generate the rest of M. (define (sieve lst) (if (null? lst) ‘() (cons (car lst) (sieve (filter (lambda (x) (not (divisible? x (car lst)))) (cdr lst))))))

מבוא מורחב - שיעור 840 What’s the time complexity of sieve? (define (sieve lst) (if (null? lst) ‘() (cons (car lst) (sieve (filter (lambda (x) (not (divisible? x (car lst)))) (cdr lst)))))) Assume lst is (list n). How many times is filter called?  (n) (number of primes < n) The Prime Number Theorem:  (n) = Θ (n/log n) (For large n the constants are nearly 1)

מבוא מורחב - שיעור 841 What’s the upper bound time complexity? (define (sieve lst) (if (null? lst) ‘() (cons (car lst) (sieve (filter (lambda (x) (not (divisible? x (car lst)))) (cdr lst)))))) Filter is called Θ (n/log n) times * O(n) per call  T(n) = O( )n 2 /log n

מבוא מורחב - שיעור 842 What’s the Lower Bound time complexity? (define (sieve lst) (if (null? lst) ‘() (cons (car lst) (sieve (filter (lambda (x) (not (divisible? x (car lst)))) (cdr lst))))))  (n/2+1..n) =  (n) -  (n/2) = Θ (n/log n) primes that are never removed from list  each such call to filter does Ω (n/log n) Work since  T(n) = Ω ( n 2 /(log n) 2 ) filter has to scan all of the list! Filter is called Θ (n/log n) times for primes p ≤ n/2

מבוא מורחב - שיעור 843 Another example Find the number of integers x in the range [1…100] s.t.: x * (x + 1) is divisible by 6. (length (filter _____________________________________ (map _________________________________ _________________________________)))) (lambda(n) (* n (+ n 1))) (integers-between 1 100) (lambda(n) (= 0 (remainder n 6))) Any bets on the result???? 66 (about two thirds)

מבוא מורחב - שיעור 844 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))))) Add up the elements of a list Multiply all the elements of a list

מבוא מורחב - שיעור 845 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)) (define (mult-all lst) (accumulate * 1 lst)) el n init op el n-1 el 1 …….. op...

Summary We saw operations on lists make then a natural data interface to work with Powerful operations on lists like accumulate and map make operating on lists even simpler מבוא מורחב - שיעור 846