6.001 SICP 1 6.001 SICP – October 7 6001-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
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
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
מבוא מורחב 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.
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
6.001 SICP SICP – October HOPs, Lists, Trees Trevor Darrell 32-D512 Office Hour: W web page:
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 Sections 5 & 6 – Oct 5, 2001 Quote & symbols Equality Quiz.
6.001 SICP SICP – October Introduction Trevor Darrell 32-D512 Office Hour: W web page:
6.001 SICP SICP – Evaluation I Recitation 11/19/2004 Eval review Evaluation examples define lambda apply New language elements.
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
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.
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)))
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.
Today’s topic: Abstraction Compound Data Data Abstractions: Isolate use of data abstraction from details of implementation Relationship between data abstraction.
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Recursive Definitions of Properties of Data Structures  Recursive.
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.
18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages
Abstraction: Procedures as Parameters CMSC Introduction to Computer Programming October 14, 2002.
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 19: Environments.
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Recursive Definitions of Properties of Data Structures  Recursive.
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.
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.
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.
Additional Scheme examples
CS 550 Programming Languages Jeremy Johnson
Representing Sets (2.3.3) Huffman Encoding Trees (2.3.4)
6.001 SICP Object Oriented Programming
Lecture 13: Quicksorting CS200: Computer Science
PPL Lecture Notes: Chapter 3 High-Order Procedures Revisited.
PPL Lazy Lists.
Lecture 7: List Recursion CS200: Computer Science
Chapter 15 – Functional Programming Languages
The role of abstractions
Closures and Streams cs784(Prasad) L11Clos
CS 270 Math Foundations of CS Jeremy Johnson
COP4020 Programming Languages
Binomial Priority Queues
6.001 SICP Data abstractions
Lecture 8: Recursion Practice CS200: Computer Science
Lecture 16: Quickest Sorting CS150: Computer Science
The Metacircular Evaluator
David Evans Lecture 9: The Great Lambda Tree of Infinite Knowledge and Ultimate Power CS200: Computer Science University.
PPL Sequence Interface.
Lecture #8 מבוא מורחב.
Streams, Delayed Evaluation and a Normal Order Interpreter
topics mutable data structures
6.001 SICP Data abstractions
Announcements Quiz 5 HW6 due October 23
Lecture #7 מבוא מורחב.
List and list operations (continue).
Today’s topics Abstractions Procedural Data
Lecture # , , , , מבוא מורחב.
Binomial Priority Queues
To understand recursion, one must first understand recursion.
list data list 만들기 list 사용하기 nil : list link :  * list -> list
Presentation transcript:

6.001 SICP SICP – October Trees Trevor Darrell 32-D512 Office Hour: W web page: section web page: Trees Re-implement pairs?

6.001 SICP 2 Trees Type Definition: Tree = List > | Leaf Leaf = C Example Operations : leaf? enum-leaves, map-tree, tree-ref, root children or subtrees

6.001 SICP 3 Example: enumerate leaves Flatten tree to list of leaves: (enumerate-leaves (list 1 (list 3 4 5)(list 2 30))) ==> ( ) (define (e-l x) (cond ((null? x) nil) ((not (pair? x)) (list x)) (else (append (e-l (car x)) (e-l (cdr x))))))

6.001 SICP 4 Example: map-tree Write a procedure, (map-tree op tree), So if tree has value (((1 2) 3) (4 (5 6)) 7 (8 9 10)) then (map-tree inc tree) has value (((2 3) 4) (5 (6 7)) 8 ( )) You can use leaf? and map.

6.001 SICP 5 map-tree (define map-tree (lambda (op tree) (if (leaf? tree) (op tree) (map ??? tree))))

6.001 SICP 6 map-tree (define map-tree (lambda (op tree) (if (leaf? tree) (op tree) (map (lambda (subtree) (map-tree op subtree)) tree))))

6.001 SICP 7 tree-ref Given (((1 2) 3) (4 (5 6)) 7 (8 9 10)) To select the element 9 out of it, we use something like (second (fourth tree)) Instead let’s define tree-ref so that we can use (tree-ref (list 3 1) tree)

6.001 SICP 8 tree-ref (define tree-ref (lambda (index tree) (if (null? index) tree (tree-ref (cdr index) (list-ref tree (car index))))))

6.001 SICP 9 Example: deep-reverse Reverse the order of a tree: (deep-reverse (list 1 (list 2 3) (list 4 5 6))) ==> ((6 5 4) (3 2) 1) (define (reverse lst) (if (null? lst) nil (append (reverse (cdr lst)) (list (car lst)))) (define (deep-reverse tree) (if (not (pair? tree)) tree (map deep-reverse (reverse tree))) )

6.001 SICP 10 General tree-manip (define (tree-manip tree init leaf first rest accum) (cond ((null? tree) init) ((not (pair? tree)) (leaf tree)) (else (accum (tree-manip (first tree) init leaf first rest accum) (tree-manip (rest tree) init leaf first rest accum))))) Given : (define test-tree (list 1 (list 2 (list 3 (list 4) 5) 6) 7)) You can write expressions using tree-manip on test-tree will subsume many of the specific function we’ve just written, e.g.,:  Flatten a tree  Deep-reverse a tree  Sum up the values of the leaves of the tree  Take the product of the even-valued leaves of the tree  Create a new tree, which keeps the odd-valued leaves of the original tree within the same tree structure, but completely removes even-valued leaves.

6.001 SICP 11 And now for something completely different…. define a new pair abstraction! The names and procedures cons, car and cdr just fell into a black hole—I can’t use them to define adjoin, first, and rest! But I can define my-cons, a HOP that does: (my-cons 1 2)  procedure ((my-cons 1 2) #t)  1 ((my-cons 1 2) #f)  2 (define my-cons (lambda (a b) (lambda (p) (if p a b)))) (define adjoin mycons) (define (first p) (p #t)) (define (rest p) (p #f)))

6.001 SICP 12 Remember… calendar…