6.001 SICP 1 6.001 SICP – October 1 6001-HOPs, Lists, Trees Trevor Darrell 32-D512 Office Hour: W 11 6.001 web page:

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

Programming with Lists
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp
מבוא מורחב 1 Lecture #7. מבוא מורחב 2 The rational number abstraction Wishful thinking: (make-rat ) Creates a rational number (numer ) Returns the numerator.
מבוא מורחב - שיעור 91 Lecture 9 Lists continued: Map, Filter, Accumulate, Lists as interfaces.
Cs7100(Prasad)L11Clos1 Closures and Streams. Contemporary Interest in Closures The concept of closures was developed in the 1960s and was first fully.
6.001 SICP SICP – October Trees Trevor Darrell 32-D512 Office Hour: W web page:
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 7 1. Outline More list examples Symbols 2.
6.001 SICP SICP – September Processes, Substitution, Recusion, and Iteration Trevor Darrell 32-D web page:
6.001 SICP SICP – October environment Trevor Darrell 32-D512 Office Hour: W web page:
6.001 SICP – September Introduction
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:
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.
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, pm GPB.
מבוא מורחב - שיעור 12 1 Lecture 12 Data directed programming Message passing dotted-tail notation & apply Section 2.4, pages ,2.5.2 pages.
6.001 SICP SICP – September Types and HOPs Trevor Darrell 32-D512 Office Hour: W web page:
( (lambda (z) (define x (lambda (x) (lambda (y z) (y x)))) ( ( (x (lambda () z)) (lambda (z) z) 3 ) ) ) 2)
מבוא מורחב 1 Lecture #8. מבוא מורחב 2 Shall we have some real fun.. Lets write a procedure scramble.. (scramble (list )) ==> ( ) (scramble.
Spring 2008Programming Development Techniques 1 Topic 6 Hierarchical Data and the Closure Property Section September 2008.
Closures and Streams More on Evaluations CS784(pm)1.
Recursion: Linear and Tree Recursive Processes and Iteration CMSC Introduction to Computer Programming October 7, 2002.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
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 You’re Invited! Course VI Freshman Open House! Friday, April 7, :30-5:00 PM FREE Course VI T-Shirts (while supplies last) and Department.
Abstraction: Procedures as Parameters CMSC Introduction to Computer Programming October 14, 2002.
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
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.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 8. Outline 1.The special form quote 2.Data abstraction: Trie 3.Alternative list: Triplets 4.Accumulate-n.
1 מבוא מורחב למדעי המחשב בשפת Scheme תרגול Outline Mutable list structure RPN calculator Vectors and sorting.
CS61A Lecture Colleen Lewis. Clicker poll Where do you think you’re learning the most? I assume you’ll learn the most in lab & disc (so.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 6. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append.
Spring 16 CSCI 4430, A Milanova/BG Ryder 1 Announcements HW5 due March 28 Homework Server link is up I will have office hours, Fri or Mon, check Announcements.
Lecture 4: Metacircles Eval Apply David Evans
Additional Scheme examples
6.001 SICP Object Oriented Programming
PPL Lecture Notes: Chapter 3 High-Order Procedures Revisited.
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Closures and Streams cs784(Prasad) L11Clos
COP4020 Programming Languages
6.001 SICP Data abstractions
Streams Sections 3.5.1,3.5.2 Pages
The Metacircular Evaluator
Lecture 18 Infinite Streams and
Lecture 11: All Sorts CS200: Computer Science University of Virginia
PPL Sequence Interface.
Lecture 18.
Lecture #8 מבוא מורחב.
6.001 SICP Streams – the lazy way
The Metacircular Evaluator (Continued)
Streams, Delayed Evaluation and a Normal Order Interpreter
topics mutable data structures
6.001 SICP Data abstractions
Lecture #7 מבוא מורחב.
List and list operations (continue).
Today’s topics Abstractions Procedural Data
Closures and Streams cs7100(Prasad) L11Clos
Lecture # , , , , מבוא מורחב.
Lecture 13: Assignment and the Environment Model (EM)
list data list 만들기 list 사용하기 nil : list link :  * list -> list
Review Previously in: Lots of language features: functions, lists, records, tuples, variants, pattern matching Today: No new language features New idioms.
Presentation transcript:

6.001 SICP SICP – October HOPs, Lists, Trees Trevor Darrell 32-D512 Office Hour: W web page: section web page: 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 )) ;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 => ( ) (map (lambda (x) (cons x nil)) y) ((1) (2) (3)) (map inc w) ( ) (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, cdr has type List ->List, and assume nil is an element of type List … What are the types of these procedures? 1. length List ->number 2.list-ref List,number->A 3.map (A->B),List ->List 4. filter (A->boolean),List ->List 5. fold-right (A,B->B),B,List ->B

6.001 SICP 11 Uniqueness Given (define x (list )) To select the unique elements from this list, ( unique x)  ( ) 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…