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

Slides:



Advertisements
Similar presentations
Analysis of Algorithms
Advertisements

מבוא מורחב 1 Lecture 4 Material in the textbook on Pages 44-46, of 2nd Edition Sections and Hanoy towers.
Recursion (define tell-story (lambda () (print ‘’Once upon a time there was a mountain. ‘’) (print ‘’On the mountain, there was a temple. ‘’) (print ‘’In.
מבוא מורחב למדעי המחשב בשפת 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.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 2. 2 Outline Scoping and block structure Recursive and iterative processes Orders of growth.
6.001 SICP – September Introduction
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 2. Reminder: Recursive algorithm Reduce problem to one or more sub-problems of smaller sizes (linear or tree.
מבוא מורחב - שיעור 4 1 Lecture 4 Order of Growth Fun with recursion  Fast exponentiation  Hanoi towers.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 11. Dotted tail notation (define (proc m1 m2. opt) ) Mandatory Arguments: m1, m2 Mandatory Arguments: m1, m2.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline High order procedures –Finding Roots –Compose Functions Accelerating Computations –Fibonacci 2.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
מבוא מורחב 1 Lecture 3 Material in the textbook Sections to
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 3. 2 Outline Let High order procedures.
מבוא מורחב 1 Lecture 3 Material in the textbook on Pages of 2nd Edition Sections to
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline High order procedures –Finding Roots –Compose Functions Accelerating Computations –Fibonacci 2.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Structure and Interpretation of Computer Programs Presented by Yan Yan CSE 294, UCSD, April 13, Chapter Harold Abelson, Gerald and Julie.
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
CMSC 2021 Recursion Recursive Definition – one that defines something in terms of itself Recursion – A technique that allows us to break down a problem.
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 13: Streams 한 태숙.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
מבוא מורחב 1 Your turn (Review) What does a lambda expression return when it is evaluated? the value of a lambda expression is a procedure What three things.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
מבוא מורחב 1 Review: scheme language things that make up scheme programs: self-evaluating 23, "hello", #t names +, pi combinations (+ 2 3) (* pi 4) special.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
February 4, 2005 Searching and Sorting Arrays. Searching.
Examples of Recursion Data Structures in Java with JUnit ©Rick Mercer.
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.
Divide & Conquer  Themes  Reasoning about code (correctness and cost)  recursion, induction, and recurrence relations  Divide and Conquer  Examples.
Week 12 - Wednesday.  What did we talk about last time?  Asymptotic notation.
Today’s topics Orders of growth of processes Relating types of procedures to different orders of growth.
Lecture 5 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Principles Of Programming Languages Lecture 2 Outline Design-By-Contract Iteration vs. Recursion Scope and binding High-order procedures.
Chapter 1: Introduction
Using Recursion to Convert Number to Other Number Bases Data Structures in Java with JUnit ©Rick Mercer.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
CS220 Programming Principles 프로그래밍의 이해 2003 가을학기 Class 2 한 태숙.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
From Lambda Calculus to LISP Functional Programming Academic Year Alessandro Cimatti
מבוא מורחב 1 Lecture 4 Material in the textbook on Pages 44-46, of 2nd Edition Sections and Hanoy towers.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 3. Outline High order procedures –Finding Roots –Compose Functions Accelerating Computations –Fibonacci 2.
1 Binding names קשירת שמות A occurrence of a name z is bound by the innermost procedure that contains the name and either 1. z is a formal parameter of.
Analyzing Programs: Order of Growth CMSC Introduction to Computer Programming October 11, 2002.
מבוא מורחב - שיעור 5 1 Lecture 5 Higher-order procedures.
Lecture #5 מבוא מורחב.
CS314 – Section 5 Recitation 10
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16.
Material in the textbook on pages
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
CS21b: Structure and Interpretation
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Algorithm design and Analysis
Lecture #5 מבוא מורחב.
Lecture 18 Infinite Streams and
Functional Programming
Lecture 18.
Binding names קשירת שמות
CS220 Programming Principles
Lecture #6 section pages pages72-77
Material in the textbook on
Lecture #6 section pages pages72-77
Lecture 5 Higher-order procedures מבוא מורחב - שיעור 5.
The structure of programming
This Lecture Substitution model
Introduction to the Lab
Lecture 6 - Recursion.
Presentation transcript:

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

Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? – Log Order of growth 2

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

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

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

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

Accelerating Computations 7

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

Slow vs Fast Expt Slow (linear) – b 0 =1 – b n =b  b n-1 Fast (logarithmic) – b n =(b 2 ) n/2 if n is even – b n =b  b n-1 if n is odd Can we do the same with Fibonacci? 9

Double Steps Fibonacci Transformation: b a a+b 2a+b 3a+2b … Double Transformation: 10

A Squaring Algorithm If we can square (or multiply) linear transformations, we have an algorithm: – Apply T n on (a,b), where: – T n =(T 2 ) n/2 If n is even – T n =T  T n-1 If n is odd 11

Squaring Transformations General Linear Transformation: Squared: 12

Iterative Algorithm Initialize: Stop condition: If count=0 return b Step count is oddcount is even 13

Representing Transformations We need to remember x, y, z, w Fibonacci Transformations belong to a simpler family: T 01 is the basic Fibonacci transformation Squaring (verify on your own!): 14

Implementation (finally) (define fib n) (fib-iter 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

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

The syntactic sugar “Let” (Let (( ) ( ).. ( )) ) ((lambda ( ….. ) ) … ) Is defined to be equivalent to: bindings 17

let* (let* (( )… ( )) ) Is equivalent to (let (( )) (let* (( )… ( )) )) 18

let vs. let* (let ((x 2) (y 3)) (let ((x 7) (z (+ x y))) (* z x))) ==> 35 19

let vs. let* (let ((x 2) (y 3)) (let* ((x 7) (z (+ x y))) (* z x))) ==> 70 20

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

Examples > (palindrome? ) #f > (palindrome? ) #t > (palindrome? 56865) #t 22

Useful functions (define (least-significant x) (remainder x 10)) (define (remove-least-significant x) (quotient x 10)) 23

Implementation 1.Construct a new number by reversing the digits of the input (x) 2.Test whether these two numbers are equal. 24

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

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

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

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

palindrome? (define (palindrome? x) (if (< x 0) #f (= x (reverse-num x)))) 29

log Consider the function defined as follows: lg(1) = 0 lg(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

Recursive process (define (lg n) (if (= n 1) 0 (+ (lg (floor (/ n 2))) 1))) 31

Iterative process (define (lg n) (define (helper n result) (if (= n 1) result (helper ( ) ( )))) (helper n 0)) 32

Iterative process (define (lg n) (define (helper n result) (if (= n 1) result (helper (floor (/ n 2)) ( )))) (helper n 0)) 33

Iterative process (define (lg n) (define (helper n result) (if (= n 1) result (helper (floor (/ n 2)) (+ result 1)))) (helper n 0)) 34

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

Order of Growth 36