6.001 SICP 1 6.001 SICP – September ? 6001-Introduction Trevor Darrell 32-D512 6.001 web page: section.

Slides:



Advertisements
Similar presentations
Lists in Lisp and Scheme a. Lists are Lisp’s fundamental data structures, but there are others – Arrays, characters, strings, etc. – Common Lisp has moved.
Advertisements

Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
1 Programming Languages and Paradigms Lisp Programming.
מבוא מורחב 1 Lecture #7. מבוא מורחב 2 The rational number abstraction Wishful thinking: (make-rat ) Creates a rational number (numer ) Returns the numerator.
מבוא מורחב 1 Lecture #6. מבוא מורחב 2 Primality testing (application) Know how to test whether n is prime in  (log n) time => Can easily find very large.
Fall 2008Programming Development Techniques 1 Topic 2 Scheme and Procedures and Processes September 2008.
מבוא מורחב - שיעור 91 Lecture 9 Lists continued: Map, Filter, Accumulate, Lists as interfaces.
6.001 SICP SICP – September Introduction Trevor Darrell 32-D web page:
6.001 SICP SICP – October Trees Trevor Darrell 32-D512 Office Hour: W web page:
6.001 SICP SICP – September Processes, Substitution, Recusion, and Iteration Trevor Darrell 32-D web page:
6.001 SICP – September Introduction
6.001 SICP SICP – October HOPs, Lists, Trees Trevor Darrell 32-D512 Office Hour: W web page:
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.
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
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:
מבוא מורחב 1 Lecture 3 Material in the textbook Sections to
SICP Data Mutation Primitive and Compound Data Mutators Stack Example non-mutating mutating Queue Example non-mutating mutating.
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, pm GPB.
6.001 SICP SICP – September Types and HOPs Trevor Darrell 32-D512 Office Hour: W web page:
מבוא מורחב 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)))
1 Lecture 15: More about assignment and the Environment Model (EM)
Spring 2008Programming Development Techniques 1 Topic 6 Hierarchical Data and the Closure Property Section September 2008.
1 Lists in Lisp and Scheme. 2 Lists are Lisp’s fundamental data structures. Lists are Lisp’s fundamental data structures. However, it is not the only.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
Recursion: Linear and Tree Recursive Processes and Iteration CMSC Introduction to Computer Programming October 7, 2002.
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.
C. Varela; Adapted w. permission from S. Haridi and P. Van Roy1 Functional Programming: Lists, Pattern Matching, Recursive Programming (CTM Sections ,
Principles Of Programming Languages Lecture 2 Today Design-By-Contract Iteration vs. Recursion.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
CS220 Programming Principles 프로그래밍의 이해 2003 가을학기 Class 2 한 태숙.
Fall 2008Programming Development Techniques 1 Topic 8 Sequences as Conventional Interfaces Section October 2008.
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.
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)
Principles Of Programming Languages Lecture 2 Today Design-By-Contract Data Structures: Lists and Pairs Iteration vs. Recursion If we have time: live.
CS314 – Section 5 Recitation 10
Functional Programming: Lists, Pattern Matching, Recursive Programming (CTM Sections , 3.2, , 4.7.2) Carlos Varela RPI September 12,
CS 550 Programming Languages Jeremy Johnson
Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16.
6.001 Jeopardy.
Pairs and Lists. Data Abstraction. SICP: Sections – 2.2.1
PPL Lecture Notes: Chapter 3 High-Order Procedures Revisited.
Racket CSC270 Pepper major portions credited to
Higher-Order Programming: Iterative computation (CTM Section 3
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Lists in Lisp and Scheme
CS 270 Math Foundations of CS Jeremy Johnson
6.001 SICP Data abstractions
Lecture 16: Quickest Sorting CS150: Computer Science
Lecture 18.
Lecture #6 section pages pages72-77
Lecture #8 מבוא מורחב.
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Lecture #6 section pages pages72-77
Mutators for compound data Stack Queue
6.001 SICP Data Mutation Primitive and Compound Data Mutators
Lecture 14: The environment model (cont
6.001 SICP Data abstractions
Lecture #7 מבוא מורחב.
List and list operations (continue).
Today’s topics Abstractions Procedural Data
Lecture # , , , , מבוא מורחב.
Lecture 13: Assignment and the Environment Model (EM)
Introduction to the Lab
Lists in Lisp and Scheme
Presentation transcript:

6.001 SICP SICP – September ? 6001-Introduction Trevor Darrell 32-D web page: section web page: Abstractions Pairs and lists Common list operations

6.001 SICP 2 Procedural abstraction example: sqrt To find an approximation of square root of x: Make a guess G Improve the guess by averaging G and x/G Keep improving the guess until it is good enough (define try (lambda (guess x) (if (good-enuf? guess x) guess (try (improve guess x) x)))) (define improve (lambda (guess x) (average guess (/ x guess)))) (define average (lambda (a b) (/ (+ a b) 2))) (define good-enuf? (lambda (guess x) (< (abs (- (square guess) x)) 0.001))) (define sqrt (lambda (x) (try 1 x)))

6.001 SICP 3 The universe of procedures for sqrt try improve average Good-enuf? sqrt

6.001 SICP 4 sqrt - Block Structure (define sqrt (lambda (x) (define good-enuf? (lambda (guess) (< (abs (- (square guess) x)) 0.001))) (define improve (lambda (guess) (average guess (/ x guess)))) (define sqrt-iter (lambda (guess) (if (good-enuf? guess) guess (sqrt-iter (improve guess))))) (sqrt-iter 1.0)) ) good-enuf? improve sqrt-iter sqrt x: number: number

6.001 SICP 5 Pairs (cons cells) (cons ) ==> ;type: x, x  Pair Where evaluates to a value, and evaluates to a value Returns a pair whose car-part is and whose cdr-part is (car ) ==> ;type: Pair  type of car part Returns the car-part of the pair (cdr ) ==> ;type: Pair  type of cdr part Returns the cdr-part of the pair

6.001 SICP 6 Pair - Box and pointer diagram (define p (cons )) p (car p) (cdr p)

6.001 SICP 7 Lists - Box and pointer diagram (cons (cons nil)) (list... ) …

6.001 SICP 8 Printed representation (define x (list 1 2)) (define y (list (list 1 2) (list 1 2)) (define z (list x x)) X == (1 2) Y == ((1 2)(1 2)) Z == ((1 2)(1 2))

6.001 SICP 9 Cons, car, cdr (define thing (cons (cons 1 nil) (cons 2 (cons 3 nil)))) thing ==> ((1) 2 3) (cons 1 nil) ==> (1) (cons 1 (cons 2 nil)) ==> (1 2) (car thing) ==> (1) (cdr thing) ==> (2 3) (car (car thing)) ==> 1 (car (cdr (cdr thing))) ==> 3

6.001 SICP 10 List drill (car (cons (+ 1 2) (- 3 4))) ==> 3 (cdr 6) ==> error (cdr (car (cons (cons 1 2) (cons 3 4)))) 2 (pair? #t) ==> #f (pair? (car (cons 1 2))) ==> #f (pair? (cons (+ 1 2) (car (cons (3 4))))) ==> #t

6.001 SICP 11 Length of a list (define (length lst) (if (null? lst) 0 (+ 1 (length (cdr lst))))

6.001 SICP 12 cdr’ing down a list (define (list-ref lst n) (if (= n 0) (car lst) (list-ref (cdr lst) (- n 1))))

6.001 SICP 13 More list drill x => (()) y => (1 2 3) z => (1 (2 3) ((4))) w => ( ) (length x) (length y) (length z) (list-ref z 2) (append x y) (cons x y)

6.001 SICP 14 More list drill x => (()) y => (1 2 3) z => (1 (2 3) ((4))) w => ( ) (length x) 1 (length y) 3 (length z) 3 (list-ref z 2) ((4)) (append x y) (() 1 2 3) (cons x y) ((()) 1 2 3)

6.001 SICP 15 Orders of Growth What is the order of growth of last-k ? (define (last-k k lst) (if (= (length lst) k) lst (last-k k (cdr lst))))

6.001 SICP 16 Orders of Growth What is the order of growth of last-k ? (define (last-k k lst) (if (= (length lst) k) lst (last-k k (cdr lst))))  ( n 2 )

6.001 SICP 17 Writing some procedures The procedure copy-some that copies the first n elements of a list (copy-some 3 (list )) ==> (1 2 3) (define (copy-some n list) )

6.001 SICP 18 Writing some procedures The procedure copy-some that copies the first n elements of a list (copy-some 3 (list )) ==> (1 2 3) (define (copy-some n list) (if (= n 0) nil (cons (car lst) (copy-some (- n 1) (cdr lst)))) )

6.001 SICP 19 Writing some procedures (repeat x m) returns a list containing the value x repeated m times. For example: (repeat 5 3) => (5 5 5) (repeat (list 1 2) 2) => ((1 2) (1 2))

6.001 SICP 20 Writing some procedures Recursive solution: (define (repeat x m) (if (= m 0) nil (cons x (repeat x (- m 1))))) time Θ(m) space Θ(m) Iterative solution: (define (repeat x m) (define (helper i answer) (if (> i m) answer (helper (+ 1 i) (cons x answer)))) (helper 0 nil)) time Θ(m) space Θ(1)

6.001 SICP 21 Writing some procedures (append lst1 lst2) appends lst2 to the end of lst1. E.g.: (append (list 0 1 2) (list 3 4)) => ( ) (append nil (list 5 6)) => (5 6)

6.001 SICP 22 Writing some procedures Recursive solution: (define (append lst1 lst2) (if (null? lst) lst2 (cons (car lst1) (append (cdr lst1) lst2)))) time Θ(n), space Θ(n) where n=(length lst1) Notice that only lst1 affects the time and space growth. Why doesn't lst2 matter?

6.001 SICP 23 Writing some procedures (reverse lst) reverses lst. E.g.: (reverse (list 0 1 2)) => (2 1 0) (reverse (list (list 3 5) (list 2 4))) => ((2 4) (3 5))

6.001 SICP 24 Writing some procedures Recursive solution: (define (reverse lst) (if (null? lst) nil (append (reverse (cdr lst)) (list (car l))))) time Θ(n^2), since append runs in linear time (Θ(m) where m=(length answer)), each successive call to append takes more and more time: n = Θ(n^2). space Θ(n), where n=(length lst) Iterative solution: (define (reverse lst) (define (helper rest answer) (if (null? rest) answer (helper (cdr rest) (cons (car rest) answer))))) (helper lst nil)) time Θ(n^2), space Θ(1), where n=(length lst)

6.001 SICP 25 Writing some procedures (num-leaves tree) returns the number of leaves in a tree, where a leaf is anything that isn't a list (number, string, boolean, procedure). E.g.: (num-leaves (list 0 1 (list 3 5) (list 2 (list 4)))) => 6 (num-leaves (list (list (list (list nil))))) => 0

6.001 SICP 26 Writing some procedures (define (num-leaves tree) (cond ((null? tree) 0) ((pair? tree) (+ (num-leaves (car tree)) (num-leaves (cdr tree))) (else 1))) time Θ(n), space Θ(d), where n is the number of pairs in the tree, and d is the depth of the tree.

6.001 SICP 27 Remember… calendar…