מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f...

36
בבבב בבבבב בבבבב בבבבב בבבבScheme ללללל4

Transcript of מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f...

Page 1: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? –

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

4תרגול

Page 2: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? –

Outline

• Repeated f• Accelerating computations

– Fibonacci

• Let and let*• Recursion examples

– Palindrome?– Log

• Order of growth

2

Page 3: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? –

3

(= n 1) f

(repeated f (- n 1))

f(x), f(f(x)), f(f(f(x))), …apply f, n times

(define (repeated f n) (if

(compose f )))

((repeated inc 5) 100) => 105((repeated square 2) 5) => 625

Repeated f

(define (compose f g)

(lambda (x) (f (g x)))) Compose now

Execute later

Page 4: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? –

4

(define (repeated f n)

(lambda (x) (repeated-iter f n x)))

Repeated f - iterative

(define (repeated-iter f n x) (if (= n 1)

(f x) (repeated-iter f (- n 1) (f x))))

Do nothing until called later

Page 5: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? –

5

Repeated f – Iterative II

(define (repeated f n)

(define (repeated-iter count accum)

(if (= count n)

accum

(repeated-iter (+ count 1)

(compose f accum))))

(repeated-iter 1 f))

Compose now

Execute later

Page 6: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? –

6

(define (smooth f) (let ((dx 0.1))

))

(define (average x y z) (/ (+ x y z) 3))

(lambda (x) (average (f (- x dx)) (f x)

(f (+ x dx))))

Smooth a function f:g(x) = (f(x – dx) + f(x) + f(x + dx)) / 3

((repeated smooth n) f)

Repeatedly smooth a function

(define (repeated-smooth f n) )

Page 7: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? –

AcceleratingComputations

7

Page 8: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? –

Iterative Fibonacci(define (fib n)

(define (fib-iter a b count)

(if (= count 0)

b

(fib-iter (+ a b) a (- count 1)))

(fib-iter 1 0 n))

• Computation time: (n)• Much better than Recursive implementation, but…• Can we do better?

8

Page 9: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? –

Slow vs Fast Expt

• Slow (linear)– b0=1– bn=bbn-1

• Fast (logarithmic)– bn=(b2)n/2 if n is even– bn=bbn-1 if n is odd

• Can we do the same with Fibonacci?

9

Page 10: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? –

Double Steps

• Fibonacci Transformation:

ab

baaT

0 1 1 2 3 5 8 13 21 b a a+b 2a+b 3a+2b …

• Double Transformation:

bab

baaT

22

10

Page 11: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? –

A Squaring Algorithm

• If we can square (or multiply) linear transformations, we have an algorithm:– Apply Tn on (a,b), where:– Tn=(T2)n/2 If n is even– Tn=TTn-1 If n is odd

11

Page 12: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? –

Squaring Transformations• General Linear Transformation:

wbzab

ybxaaT wzyx ,,,

• Squared:

22

2

2

2,,,

wyzzwxzywxyyzx

wzyx

T

bwyz

azwxz

wbzaw

ybxazb

bywxy

ayzx

wbzay

ybxaxa

T

12

Page 13: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? –

Iterative Algorithm• Initialize:

ab

baaTncountba 01

• Stop condition: If count=0 return b

• Step

2/1

,, 2

countcountcountcount

TTbaTba

count is odd count is even

13

Page 14: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? –

Representing Transformations

• We need to remember x, y, z, w• Fibonacci Transformations belong to a simpler

family:

aqbpb

apaqbqaTpq

• T01 is the basic Fibonacci transformation• Squaring (verify on your own!):

222 2

2

qpqqppq TT

14

Page 15: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? –

Implementation (finally)(define fib n)

(fib-iter 1 0 0 1 n))

(define (fib-iter a b p q count)

(cond ((= count 0) b)

((even? count)

(fib-iter a

b

(/ count 2)

(else (fib-iter

p

q

(- count 1))))

(+ (square p) (square q))

(+ (* 2 p q) (square q))

(+ (* b q) (* a q) (* a p))

(+ (* b p) (* a q))

15

Page 16: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? –

The syntactic sugar “Let”(define (f x y)

( define (f-helper a b) *( +( x (square a))

*( y b) *( a b)))

( f-helper (+ 1 (* x y)) -( 1 y)))

(define (f x y)(( lambda (a b) *( +( x (square a))

*( y b) *( a b)))

+( 1 *( x y)) -( 1 y)))

(define (f x y)( let ((a (+ 1 (* x y)))

( b (- 1 y))) *( +( x (square a))

*( y b) *( a b))))

16

Page 17: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? –

The syntactic sugar “Let”(Let ((<var1> <exp1>)

<( var2> <exp2)>

..

<( varn> <expn))>

< body)>

((lambda (<var1> ….. <varn>)

< body)>

< exp1>

< exp2>

< expn)>

Is defined to be equivalent to:

bindings

17

Page 18: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? –

let*

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

<body>)

Is equivalent to

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

18

Page 19: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? –

let vs. let*

(let ((x 2) (y 3)) (let ((x 7)

(z (+ x y))) (* z x))) ==> 35

19

Page 20: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? –

let vs. let*

(let ((x 2) (y 3)) (let* ((x 7)

(z (+ x y))) (* z x))) ==> 70

20

Page 21: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? –

palindrome?

• Palindromes are (positive) numbers that read the same in both directions (e.g. left to right and right to left).

• Write a procedure (palindrome? x) that gets an integer as parameter and returns true (#t) if the number is a palindrome and false (#f) otherwise.

21

Page 22: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? –

Examples

> (palindrome? 1234567)#f> (palindrome? 1234321)#t> (palindrome? 56865)#t

22

Page 23: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? –

Useful functions

(define (least-significant x) (remainder x 10))

(define (remove-least-significant x) (quotient x 10))

23

Page 24: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? –

Implementation

1. Construct a new number by reversing the digits of the input (x)

2. Test whether these two numbers are equal.

24

Page 25: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? –

reverse-num

(define (reverse-num x) (define (helper x factor)(if (< x 10) x (+ (* ( ) factor)

(helper ( ) (/ factor 10)))))

(define (factor x) (if (< x 10)

1 (* 10 (factor ( )))))

(helper x (factor x)))

25

Page 26: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? –

reverse-num

(define (reverse-num x) (define (helper x factor)(if (< x 10) x (+ (* (least-significant x) factor)

(helper ( ) (/ factor 10)))))

(define (factor x) (if (< x 10)

1 (* 10 (factor ( )))))

(helper x (factor x)))

26

Page 27: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? –

reverse-num

(define (reverse-num x) (define (helper x factor)(if (< x 10) x (+ (* (least-significant x) factor)

(helper (remove-least-significant x) (/ factor 10))))) (define (factor x)

(if (< x 10) 1

(* 10 (factor ( )))))

(helper x (factor x)))

27

Page 28: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? –

reverse-num

(define (reverse-num x) (define (helper x factor)(if (< x 10) x (+ (* (least-significant x) factor)

(helper (remove-least-significant x) (/ factor 10))))) (define (factor x)

(if (< x 10) 1 (* 10 (factor (remove-least-significant x)))))

(helper x (factor x)))

28

Page 29: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? –

palindrome?

(define (palindrome? x) (if (< x 0)

#f (= x (reverse-num x))))

29

Page 30: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? –

log

Consider the function defined as follows: lg(1) = 0lg(n) = lg( n/2 ) + 1, n > 1⌊ ⌋ *Do not use mathematical functions not shown here, such as expt or sqrt. *You may use (floor x) to compute x .⌊ ⌋

30

Page 31: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? –

Recursive process 

(define (lg n) (if (= n 1)

0(+ (lg (floor (/ n 2))) 1)))

31

Page 32: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? –

Iterative process

(define (lg n) (define (helper n result)

(if (= n 1)result(helper ( ) ( ))))

(helper n 0))

32

Page 33: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? –

Iterative process

(define (lg n) (define (helper n result)

(if (= n 1)result(helper (floor (/ n 2))

( )))) (helper n 0))

33

Page 34: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? –

Iterative process

(define (lg n) (define (helper n result)

(if (= n 1)result(helper (floor (/ n 2)) (+ result 1))))

(helper n 0))

34

Page 35: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? –

Order of Growth

• For each of the following statements, determine whether it is true or false. If it is true, provide a proof, if false, provide a counterexample.

• – Θ(2^n) = Θ(3^n)– 2^(3*lg n+2) = O(n^3)

35

Page 36: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? –

Order of Growth

36