{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

View Full Document Right Arrow Icon
Caltech CS 1 -- Fall 2008 CS1: Introduction to Computation Day 4: October 8, 2008 Dynamic Evolution of Computations
Background image of page 1

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

View Full Document Right Arrow Icon
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!
Background image of page 2
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)
Background image of page 3

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

View Full Document Right Arrow Icon
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!
Background image of page 4
Caltech CS 1 -- Fall 2008 Example Recall from last time: (define (sum-integers n) (if (= n 0) 0 (+ n (sum-integers (- n 1)))))
Background image of page 5

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

View Full Document Right Arrow Icon
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))))))
Background image of page 6
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
Background image of page 7

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

View Full Document Right Arrow Icon
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)))
Background image of page 8
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)))
Background image of page 9

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

View Full Document Right Arrow Icon
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)
Background image of page 10
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)))
Background image of page 11

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

View Full Document Right Arrow Icon
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
Background image of page 12
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?
Background image of page 13

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

View Full Document Right Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}