+ All Categories
Home > Documents > מבוא מורחב למדעי המחשב בשפת Scheme

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

Date post: 12-Jan-2016
Category:
Upload: foy
View: 50 times
Download: 6 times
Share this document with a friend
Description:
מבוא מורחב למדעי המחשב בשפת Scheme. תרגול 4. Outline. Let* List and pairs manipulations Insertion Sort Abstraction Barriers Fractals Mobile. let*. (let* (( )… ( )) ) is equivalent to (let (( )) (let* (( )… - PowerPoint PPT Presentation
Popular Tags:
32
בבבב בבבבב בבבבב בבבבב בבבבScheme ללללל4
Transcript
Page 1: מבוא מורחב למדעי המחשב בשפת  Scheme

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

4תרגול

Page 2: מבוא מורחב למדעי המחשב בשפת  Scheme

Outline

• Let*

• List and pairs manipulations– Insertion Sort

• Abstraction Barriers – Fractals– Mobile

2

Page 3: מבוא מורחב למדעי המחשב בשפת  Scheme

let*

(let* ((<var1> <exp1>)…(<varn> <expn>))

<body>)

is equivalent to

(let ((<var1> <exp1>)) (let* ((<var2> <exp2>)… (<varn> <expn>)) <body>))

3

Page 4: מבוא מורחב למדעי המחשב בשפת  Scheme

4

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

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

5

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

1 2

Page 6: מבוא מורחב למדעי המחשב בשפת  Scheme

6

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

1

4

32

5 6 7

8

Page 7: מבוא מורחב למדעי המחשב בשפת  Scheme

7

(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))))

15 4

23

How to reach the 3 with cars and cdrs?

)car (car (cdr (cdr x)))(

Page 8: מבוא מורחב למדעי המחשב בשפת  Scheme

8

cdr-ing down a listcons-ing up a list

(add-sort 4 (list 1 3 5 7 9))(1 3 4 5 7 9)(add-sort 5 ‘())(5)(add-sort 6 (list 1 2 3))(1 2 3 6)

(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

Page 9: מבוא מורחב למדעי המחשב בשפת  Scheme

9

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

• (7 3 5 9 1)• (3 5 9 1)• (5 9 1)• (9 1)• (1)• ()

(1 3 5 7 9)(1 3 5 9)(1 5 9)(1 9)(1)()

Time Complexity?

Page 10: מבוא מורחב למדעי המחשב בשפת  Scheme

10

Implementation

(define (insertion-sort s)

(if (null? s) null

(add-sort (car s)

(insertion-sort (cdr s)))))

Page 11: מבוא מורחב למדעי המחשב בשפת  Scheme

11

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.

Page 12: מבוא מורחב למדעי המחשב בשפת  Scheme

12

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

Page 13: מבוא מורחב למדעי המחשב בשפת  Scheme

13

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] ))))

Page 14: מבוא מורחב למדעי המחשב בשפת  Scheme

14

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:

Page 15: מבוא מורחב למדעי המחשב בשפת  Scheme

15

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:

Page 16: מבוא מורחב למדעי המחשב בשפת  Scheme

16

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 )

(sierpinski )))))))

(make-triangle a a-b c-a))

(make-triangle b a-b b-c))

(make-triangle c c-a b-c))

Page 17: מבוא מורחב למדעי המחשב בשפת  Scheme

17

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

Page 18: מבוא מורחב למדעי המחשב בשפת  Scheme

18

Mobile

Page 19: מבוא מורחב למדעי המחשב בשפת  Scheme

19

Mobile

• Left and Right branches

• Constructor– (make-mobile left right)

• Selectors– (left-branch mobile)– (right-branch mobile)

Page 20: מבוא מורחב למדעי המחשב בשפת  Scheme

20

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)

Page 21: מבוא מורחב למדעי המחשב בשפת  Scheme

21

Building mobiles

61 2

(define m

(make-mobile

(make-branch 4 6)

(make-branch

8

(make-mobile

(make-branch 4 1)

(make-branch 2 2)))))

24

4 8

Page 22: מבוא מורחב למדעי המחשב בשפת  Scheme

22

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

Page 23: מבוא מורחב למדעי המחשב בשפת  Scheme

23

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))

Page 24: מבוא מורחב למדעי המחשב בשפת  Scheme

24

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

Page 25: מבוא מורחב למדעי המחשב בשפת  Scheme

25

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)

6

12

15

4 8

Page 26: מבוא מורחב למדעי המחשב בשפת  Scheme

26

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)

Page 27: מבוא מורחב למדעי המחשב בשפת  Scheme

27

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(n2)

• Lower bound

Page 28: מבוא מורחב למדעי המחשב בשפת  Scheme

28

Mobile structures

n

n-1

n-2

n-3

. . .

T(n) = T(n-1) + (n) (for this family of mobiles)

T(n) = (n2)

Page 29: מבוא מורחב למדעי המחשב בשפת  Scheme

29

Mobile structures

n/2

T(n) = 2T(n/2) + (n) (for this family of mobiles)

T(n) = (nlogn)

n/2

n/4 n/4 n/4 n/4

n/8 n/8 n/8 n/8 n/8 n/8 n/8 n/8

Page 30: מבוא מורחב למדעי המחשב בשפת  Scheme

30

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))

Page 31: מבוא מורחב למדעי המחשב בשפת  Scheme

31

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)))

Page 32: מבוא מורחב למדעי המחשב בשפת  Scheme

32

Complexity revised

• Complexity of new total-weight?

• Complexity of new constructor?

• Complexity of balanced?

• Can we do even better?


Recommended