מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. Outline Abstraction Barriers –Fractals –Mobile List and pairs manipulations –Insertion Sort 2.

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

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.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
מבוא מורחב 1 Lecture #7. מבוא מורחב 2 The rational number abstraction Wishful thinking: (make-rat ) Creates a rational number (numer ) Returns the numerator.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? – Log.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. Outline Let* List and pairs manipulations –Insertion Sort Abstraction Barriers –Fractals –Mobile 2.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 10. Environment Model 3.2, pages
מבוא מורחב - שיעור 91 Lecture 9 Lists continued: Map, Filter, Accumulate, Lists as interfaces.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 7 1. Outline More list examples Symbols 2.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
מבוא מורחב - שיעור 10 1 Symbols Manipulating lists and trees of symbols: symbolic differentiation Lecture 10.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 11. Dotted tail notation (define (proc m1 m2. opt) ) Mandatory Arguments: m1, m2 Mandatory Arguments: m1, m2.
6.001 SICP SICP – September ? 6001-Introduction Trevor Darrell 32-D web page: section.
Today - Limits of computation - Complexity analysis examples.
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).
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.
An Introduction to Fractals By: Brian Feuer What is a Fractal? A word coined by Benoit Mandelbrot in 1975 to describe shapes that are “self-similar”
Arbitrarily Long Data Structures: Lists and Recursion CMSC Introduction to Computer Programming October 4, 2002.
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.
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.
Compound Data. Last Lecture Booleans Only two elements Symbols Not to be confused with variables Strings Notions of equality on values.
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.
18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages
מבוא מורחב 1 Lecture #9. מבוא מורחב 2 Symbol: a primitive type constructors: (quote alpha) ==> quote is a special form. One argument: a name. selectors.
Spring 2004Programming Development Techniques 1 Topic 11 Sets and their Representation April 2004.
Area of a Plane Region We know how to find the area inside many geometric shapes, like rectangles and triangles. We will now consider finding the area.
1 Data Abstraction. Pairs and Lists. (SICP Sections – 2.2.1)
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 8. Outline 1.The special form quote 2.Data abstraction: Trie 3.Alternative list: Triplets 4.Accumulate-n.
1/32 Data Mutation Primitive and compound data mutators set! for names set-car!, set-cdr! for pairs Stack example non-mutating mutating Queue example non-mutating.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 6. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append.
מבוא מורחב שיעור 7 1 Lecture 7 Data Abstraction. Pairs and Lists. (Sections – 2.2.1)
1 Vectors, binary search, and sorting. 2 We know about lists O(n) time to get the n-th item. Consecutive cons cell are not necessarily consecutive in.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Functional Programming Language 1 Scheme Language: part 3.
Midpoint and Distance in the Coordinate Plane
Data Structures.
Additional Scheme examples
Edited by Original material by Eric Grimson
CS 550 Programming Languages Jeremy Johnson
Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16.
Representing Sets (2.3.3) Huffman Encoding Trees (2.3.4)
Pairs and Lists. Data Abstraction. SICP: Sections – 2.2.1
CS 1321.
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Lists in Lisp and Scheme
CS 270 Math Foundations of CS Jeremy Johnson
COP4020 Programming Languages
6.001 SICP Data abstractions
1-6 Midpoint & Distance in the Coordinate Plane
Lisp Tutorial Click on Xlisp icon – you enter the interpreter
Lecture #8 מבוא מורחב.
Lecture 13 - Assignment and the environments model Chapter 3
Data Mutation Primitive and compound data mutators set! for names
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
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).
6.001: Structure and Interpretation of Computer Programs
Today’s topics Abstractions Procedural Data
Functional Programming: Lisp
Lecture # , , , , מבוא מורחב.
Lecture 13: Assignment and the Environment Model (EM)
Lists in Lisp and Scheme
Presentation transcript:

מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5

Outline Abstraction Barriers –Fractals –Mobile List and pairs manipulations –Insertion Sort 2

3 cons, car, cdr, list (cons 1 2) is a pair => (1. 2) box and pointer diagram: nil = () the empty list (null in Dr. Scheme) (list 1) = (cons 1 nil) => (1) 1 2 1

4 (car (list 1 2)) => 1 (cdr (list 1 2)) => (2) (cadr (list 1 2)) => 2 (cddr (list 1 2)) => () 1 2

5 (list 1 (list (list 2 3) 4) (cons 5 (list 6 7)) 8)

6 (5 4 (3 2) 1) (list 5 4 (list 3 2) 1) (cons 5 (cons 4 (cons (cons 3 (cons 2 nil)) (cons 1 null)))) How to reach the 3 with cars and cdrs? (car (car (cdr (cdr x))))

7 cdr-ing down a list cons-ing up a list (add-sort 4 (list ))  ( ) (add-sort 5 ‘())  (5) (add-sort 6 (list 1 2 3))  ( ) (define (add-sort n s) (cond ((null? s) ) ((< n (car s)) ) (else ))) (list n) (cons n s) (cons (car s) (add-sort n (cdr s))) cons-ing up cdr-ing down

8 Insertion sort An empty list is already sorted To sort a list with n elements: –Drop the first element –Sort remaining n-1 elements (recursively) –Insert the first element to correct place ( ) ( ) (5 9 1) (9 1) (1) () ( ) ( ) (1 5 9) (1 9) (1)(1) () Time Complexity?

9 Implementation (define (insertion-sort s) (if (null? s) null (add-sort (car s) (insertion-sort (cdr s)))))

10 Fractals Definitions: A mathematically generated pattern that is reproducible at any magnification or reduction. A self-similar structure whose geometrical and topographical features are recapitulated in miniature on finer and finer scales. An algorithm, or shape, characterized by self-similarity and produced by recursive sub-division.

11 Sierpinski triangle Given the three endpoints of a triangle, draw the triangle Compute the midpoint of each side Connect these midpoints to each other, dividing the given triangle into four triangles Repeat the process for the three outer triangles

12 Sierpinski triangle – Scheme version (define (sierpinski triangle) (cond ((too-small? triangle) #t) (else (draw-triangle triangle) (sierpinski [outer triangle 1] ) (sierpinski [outer triangle 2] ) (sierpinski [outer triangle 3] ))))

13 Scheme triangle (define (make-triangle a b c) (list a b c)) (define (a-point triangle) (car triangle)) (define (b-point triangle) (cadr triangle)) (define (c-point triangle) (caddr triangle)) (define (too-small? triangle) (let ((a (a-point triangle)) (b (b-point triangle)) (c (c-point triangle))) (or (< (distance a b) 2) (< (distance b c) 2) (< (distance c a) 2)))) (define (draw-triangle triangle) (let ((a (a-point triangle)) (b (b-point triangle)) (c (c-point triangle))) (and ((draw-line view) a b my-color) ((draw-line view) b c my-color) ((draw-line view) c a my-color)))) Constructor: Selectors: Predicate: Draw:

14 Points (define (make-posn x y) (list x y)) (define (posn-x posn) (car posn)) (define (posn-y posn) (cadr posn)) (define (mid-point a b) (make-posn (mid (posn-x a) (posn-x b)) (mid (posn-y a) (posn-y b)))) (define (mid x y) (/ (+ x y) 2)) (define (distance a b) (sqrt (+ (square (- (posn-x a) (posn-x b))) (square (- (posn-y a) (posn-y b)))))) Constructor: Selectors:

15 Sierpinski triangle – Scheme final version (define (sierpinski triangle) (cond ((too-small? triangle) #t) (else (let ((a (a-point triangle)) (b (b-point triangle)) (c (c-point triangle))) (let ((a-b (mid-point a b)) (b-c (mid-point b c)) (c-a (mid-point c a))) (and (draw-triangle triangle) (sierpinski ) (sierpinski ))))))) (make-triangle a a-b c-a)) (make-triangle b a-b b-c)) (make-triangle c c-a b-c))

16 Abstraction barriers Programs that use Triangles Too-small? draw-triangle make-posn posn-x posn-y cons list car cdr Triangles in problem domain Points as lists of two coordinates (x,y) Points as lists make-triangle a-point b-point c-point Triangles as lists of three points

17 Mobile

18 Mobile Left and Right branches Constructor –(make-mobile left right) Selectors –(left-branch mobile) –(right-branch mobile)

19 Branch Length and Structure –Length is a number –Structure is… Another mobile A leaf (degenerate mobile) –Weight is a number Constructor –(make-branch length structure) Selectors –(branch-length branch) –(branch-structure branch)

20 Building mobiles (define m (make-mobile (make-branch 4 6) (make-branch 8 (make-mobile (make-branch 4 1) (make-branch 2 2))))) 24 48

21 Mobile weight A leaf’s weight is its value A mobile’s weight is: –Sum of all leaves = –Sum of weights on both sides (total-weight m) –9 (6+1+2) 6 1 2

22 Mobile weight (define (total-weight mobile) (if (atom? mobile) mobile (+ (total-weight ) (total-weight ) ))) (define (atom? x) (and (not (pair? x)) (not (null? x)))) (branch-structure (left-branch mobile)) (branch-structure (right-branch mobile))

23 Complexity Analysis What does “n” represent? –Number of weights? –Number of weights, sub-mobiles and branches? –Number of pairs? –All of the above? Analysis –  (n) –Depends on mobile’s size, not structure

24 Balanced mobiles Leaf –Always Balanced Rod –Equal moments –F = length x weight Mobile –All rods are balanced = –Main rod is balanced, and both sub-mobiles (balanced? m)

25 balanced? (define (balanced? mobile) (or (atom? mobile) (let ((l (left-branch mobile)) (r (right-branch mobile))) (and (= ) (balanced? ) (balanced? ))))) (* (branch-length l) (total-weight (branch-structure l))) (* (branch-length r) (total-weight (branch-structure r))) (branch-structure l) (branch-structure r)

26 Complexity Worst case scenario for size n –Need to test all rods –May depend on mobile structure Upper bound –Apply total-weight on each sub-mobile –O(n 2 ) Lower bound

27 Mobile structures n n-1 n-2 n-3... T(n) = T(n-1) +  (n)(for this family of mobiles) T(n) =  (n 2 )

28 Mobile structures n/2 T(n) = 2T(n/2) +  (n)(for this family of mobiles) T(n) =  (nlogn) n/2 n/4 n/8

29 Implementation Constructors (define (make-mobile left right) (list left right)) (define (make-branch length structure) (list length structure)) Selectors (define (left-branch mobile) (car mobile)) (define (right-branch mobile) (cadr mobile)) (define (branch-length branch) (car branch)) (define (branch-structure branch) (cadr branch))

30 Preprocessing the data Calculate weight on creation: –(define (make-mobile left right) (list left right (+ (total-weight (branch-structure left)) (total-weight (branch-structure right))))) New Selector: –(define (mobile-weight mobile) (caddr mobile)) Simpler total-weight: –(define (total-weight mobile) (if (atom? mobile) mobile (mobile-weight mobile)))

31 Complexity revised Complexity of new total-weight? Complexity of new constructor? Complexity of balanced? Can we do even better?