{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

day04

# day04 - CS1 Introduction to Computation Day 4 October 8...

This preview shows pages 1–14. Sign up to view the full content.

Caltech CS 1 -- Fall 2008 CS1: Introduction to Computation Day 4: October 8, 2008 Dynamic Evolution of Computations

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Caltech CS 1 -- Fall 2008 Before we begin... Let's select house ombudspeople! Meet at noon on Thursday at the Athenaeum (olive walk entrace) Starting tomorrow!
Caltech CS 1 -- Fall 2008 Today, let’s think about: How do our computations unfold? – in space – in time This will lead us to consider: efficiency of program execution (first look at a huge subject)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Caltech CS 1 -- Fall 2008 Design and analysis (again) Last time saw how to design recursive procedures There are many ways to design a procedure that solves the same problem Analysis will help us to distinguish "good" solutions from "bad" ones – based on some criterion for "good" and "bad" Design and analysis always interact!
Caltech CS 1 -- Fall 2008 Example Recall from last time: (define (sum-integers n) (if (= n 0) 0 (+ n (sum-integers (- n 1)))))

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Caltech CS 1 -- Fall 2008 Revisited (summarizing steps) Evaluate: (sum-integers 3) (if (= 3 0) 0 (+ 3 (sum-integers (- 3 1)))) • (if #f 0 (+ 3 (sum-integers (- 3 1)))) (+ 3 (sum-integers (- 3 1))) (+ 3 (sum-integers 2)) (+ 3 (if (= 2 0) 0 (+ 2 (sum-integers (- 2 1))))) (+ 3 (+ 2 (sum-integers 1))) (+ 3 (+ 2 (if (= 1 0) 0 (+ 1 (sum-integer (- 1 1))))))
Caltech CS 1 -- Fall 2008 Revisited (summarizing steps) (+ 3 (+ 2 (+ 1 (sum-integers 0)))) (+ 3 (+ 2 (+ 1 (if (= 0 0) 0 …)))) (+ 3 (+ 2 (+ 1 (if #t 0 …)))) (+ 3 (+ 2 ( + 1 0))) • … • 6

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Caltech CS 1 -- Fall 2008 Evolution of computation (sum-integers 3) (+ 3 (sum-integers 2)) (+ 3 (+ 2 (sum-integers 1))) (+ 3 (+ 2 (+ 1 (sum-integers 0)))) (+ 3 (+ 2 (+ 1 0)))
Caltech CS 1 -- Fall 2008 What can we say about the computation? On input N : How many calls to sum-integers ? N+1 How much work per call? (sum-integers 3) (+ 3 (sum-integers 2)) (+ 3 (+ 2 (sum-integers 1))) (+ 3 (+ 2 (+ 1 (sum-integers 0)))) (+ 3 (+ 2 (+ 1 0)))

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Caltech CS 1 -- Fall 2008 Work per call? (+ 3 (sum-integers 2)) (+ 3 (if (= 2 0) 0 (+ 2 (sum-integers (- 2 1))))) (+ 3 (+ 2 (sum-integers 1))) compare if add * add call *(subtract costs same as add)
Caltech CS 1 -- Fall 2008 What can we say about the computation? on input N : How many calls to sum-integers ? N+1 How much work per call? constant one comparison, one if , two additions, one function call (at most) How many deferred operations at end? (sum-integers 3) (+ 3 (sum-integers 2)) (+ 3 (+ 2 (sum-integers 1))) (+ 3 (+ 2 (+ 1 (sum-integers 0)))) (+ 3 (+ 2 (+ 1 0)))

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Caltech CS 1 -- Fall 2008 Deferred operations? (+ 3 (+ 2 (+ 1 0))) Last call to sum-integers gives the result 0 – base case but have to use this result to "finish up" computations that were previously started to get final result We call those incomplete computations deferred operations or pending operations
Caltech CS 1 -- Fall 2008 What can we say about the computation? on input N : How many calls to sum-integers ? N+1 How much work per call? constant one comparison, one if , two additions, one function call (at most) How many deferred operations at end?

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}