1 / 13

Mastering Higher-order Procedures with Lists and Trees

Explore the rights and privileges of first-class citizens in Scheme through higher-order procedures and learn patterns like transforming, filtering, and accumulating results in lists. Dive into unique element selection using functional programming techniques.

jtrevor
Download Presentation

Mastering Higher-order Procedures with Lists and Trees

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. 6.001 SICP – October 1 6001-HOPs, Lists, Trees Trevor Darrell trevor@csail.mit.edu 32-D512 Office Hour: W 11 6.001 web page: http://sicp.csail.mit.edu/ section web page: http://www.csail.mit.edu/~trevor/6001/ • More Higher-order procedures • HOPs and Lists 6.001 SICP

  2. Summary of higher order procedures Rights and privileges of first class citizens • May be named by variables • May be passed as arguments to procedures • May be returned as results of procedures • May be included as parts of data structures Procedures in Scheme are + first class citizens! 6.001 SICP

  3. Repeated power rather than: (define fourth-power (composef square square)) (define eight-power (composef square (composef square square))) let’s use… (define fourth-power (repeated square 2)) (define eight-power (repeated square 3)) (define composef (lambda (f g) (lambda (x) (f (g x)))) ) 6.001 SICP

  4. Recusive repeated (define fourth-power (repeated square 2)) (define (repeated proc n) (if (= n 0) (lambda (x) x) (composef proc (repeated proc (- n 1))))) (define fourth-power (repeated square 2)) (repeated square 2) (composef square (repeated square 1)) (composef square (composef square (repeated square 0))) (composef square (composef square (lambda (x) x))) ... 6.001 SICP

  5. Iterative repeated (define (repeated proc n) (define (iter n ans) (if (= n 0) ans (iter (- n 1) (composef f ans)))) (iter n (lambda (x) x))) 6.001 SICP

  6. List HOP Common Pattern #1: Transforming a List (define (square-list lst) (if (null? lst) nil (adjoin (square (first lst)) (square-list (rest lst))))) (define (double-list lst) (if (null? lst) nil (adjoin (* 2 (first lst)) (double-list (rest lst))))) (define (square-list lst) (map square lst)) (define (double-list lst) (map (lambda (x) (* 2 x)) lst)) (define (MAP proc lst) (if (null? lst) nil (adjoin (proc (first lst)) (map proc (rest lst))))) 6.001 SICP

  7. List HOP Common Pattern #2: Filtering a List (filter even? (list 1 2 3 4 5 6)) ;Value: (2 4 6) (define (filter pred lst) (cond ((null? lst) nil) ((pred (first lst)) (adjoin (first lst) (filter pred (rest lst)))) (else (filter pred (rest lst))))) 6.001 SICP

  8. List HOP Common Pattern #3: Accumulating Results (define (add-up lst) (if (null? lst) 0 (+ (first lst) (add-up (rest lst))))) (define (mult-all lst) (if (null? lst) 1 (* (first lst) (mult-all (rest lst))))) (define (add-up lst) (fold-right + 0 lst)) (define (FOLD-RIGHT op init lst) (if (null? lst) init (op (first lst) (fold-right op init (rest lst))))) 6.001 SICP

  9. Lists and higher-order procedures x => (()) y => (1 2 3) z => (1 (2 3) ((4))) w => (1 2 3 4 5) (map (lambda (x) (cons x nil)) y) ((1) (2) (3)) (map inc w) (2 3 4 5 6) (filter odd? w) (1 3 5) (map inc (filter odd? w)) (2 4 6) (filter odd? (map inc w)) (3 5) (fold-right + 0 (map inc (filter odd? w))) 12 6.001 SICP

  10. For lists, car has type List<A>->A, cdr has type List<A>->List<A> , and assume nil is an element of type List<A>… What are the types of these procedures? 1.length List<A>->number 2.list-ref List<A>,number->A 3.map (A->B),List<A>->List<B> 4.filter (A->boolean),List<A>->List<A> 5.fold-right (A,B->B),B,List<A>->B 6.001 SICP

  11. Uniqueness Given (define x (list 1 2 2 3 1 5 4 3 4)) To select the unique elements from this list, (unique x)  (1 2 3 5 4) Start with function remove (e.g. (remove 3 (list 3 4 5))  (4 5) (define (remove el lst) (filter (lambda (x) (not (= x el))) lst) 6.001 SICP

  12. Uniqueness (define (unique lst) (if (null? lst) nil (cons (car seq) (unique (remove (car lst) (cdr lst)))) 6.001 SICP

  13. Remember… • calendar… 6.001 SICP

More Related