2/27 ! revised tracing examples ! matlab code to generate pictures for trace announcements + Course & T1 evaluation form activated for about 1 week, starting today + E7 posted later today; due online 8am Thursday + P3 solutions posted later day + must choose Project partners before submission deadline reminders + do not violate Academic Integrity: + you may work with the same partner for the entire Project + you may *not* work closely with more people than that + run code in Lecture Sketches + P4; e-mail submissions are *not* accepted topics + tracing: extend box scope diagram memory model to deal with functions _______________________________________________________________________________ Course & T1 evaluation overview + use *same* random, anonymous, 8-digit ID on all 5 parts: + be sure to save it! + remark: how many digits are required to avoid "ID collisions"? birthday problem again! _______________________________________________________________________________ birthday problem ! recall code: ! ! >> nbuckets = 365; ndarts = 200; ! >> bucket = zeros(1,nbuckets); ! >> for i = 1:ndarts ! >> dart = ceil(rand*nbuckets); ! >> bucket(dart) = 1+bucket(dart); ! >> end ! ! maybe find the day $birthday$ with largest frequency $maxfreq$: ! ! >> [maxfreq, birthday] = max(bucket); ! ! useful computation (shared birthday, shared lecture question, ID collision) ! + therefore, want to reuse it ! ! non-trivial computation (random numbers, counting, loops, (efficiency)) ! + therefore, want easy way to reuse it ! + rewrite/rederive: hard! duplicated effort! ! + copy/paste: hard! duplicated effort! ! (need to read and understand code to rename variables to avoid conflicts) ! ! solution: package code as a function with well-chosen name ! + a well-chosen name is easier to remember and understand than code ! + a function has its own scope (scratch paper), so no variable name conflicts ! + if later find a way to improve code, only need to change function definition ! to propagate improvements ! (if used copy/paste, would need to modify each place where it is used -- ! could be many places and could require reading the client code to ! figure out safe variables to use.) ! ! calling code: ! ! >> [maxfreq, birthday] = max(throwdarts(200, 365)); ! ! function file $throwdarts.m$: ! ! +--------------------------------------------------------------------------+ ! | function bucket = throwdarts(ndarts,nbuckets) | ! | % throwdarts: randomly throw darts into buckets | ! | % bucket = darts(ndarts,nbuckets): | ! | % randomly throw $ndarts$ darts into $nbuckets$ buckets $1..nbuckets$; | ! | % $bucket(i)$ = number of darts landing in bucket $i$. | ! | | ! | bucket = zeros(1,nbuckets); | ! | for i = 1:ndarts | ! | dart = ceil(rand*nbuckets); | ! | bucket(dart) = 1+bucket(dart); | ! | end | ! +--------------------------------------------------------------------------+ _______________________________________________________________________________ box scope diagram memory model for tracing + (review 1/25 lecture if you don't remember the box diagram memory model) + extend to include function calls + basically, a summary of the analogy from the 2/22 lecture scope + a *scope* = a collection of identifiers with the same "visibility" + in analogy: a piece of scrap paper + in box scope memory model: a box containing identifiers + scopes can be *nested* + code runs within a scope scope rules -- to find an identifier (variable or function name): + look inside current scope (but do not look inside scopes within the current scope) + if find identifier, then done + else move to immediately enclosing scope. repeat search. + if there is no enclosing scope, then identifier is not found declaring (creating) a variable + create variable inside original scope (where search started) initial scopes: + a BIG enclosing scope with all the names of functions + usually implicit, rather than explicit: + too much trouble to draw all functions, pre-defined and user-defined + command window scope + (conceptually:) immediately within BIG enclosing scope + statements executed in command window run inside command window scope, not the BIG enclosing scope function call 1. draw activation record (function name, box) inside the scope where function is found 2. evaluate arguments at same scope the function is found (for now: immediately inside BIG scope) 3. inside activation record: write in parameters, fill in with argument values (1st parameter <-- 1st value, 2nd <-- 2nd, 3rd <-- 3rd, etc.) 4. run function body inside activation record 5. copy out return values 6. tear down activation record: cross out do not confuse *printing* a value with *returning* a value _______________________________________________________________________________ notes + see f-g-trace.txt and fib-trace.txt for tracing examples + download $strhcat.m$ (used by $boxscope$) and $boxscope.m$ to help you draw your own box scope diagram pictures + see f-g-trace.txt and fib-trace.txt for examples of how to use $boxscope$ _______________________________________________________________________________ Q: if you look for variable $a$ and do not find it in scope, but do Q: find it in a larger scope, where is the new value assigned? A: where you find it: the larger scope. Q: in $fib(n)$, if $n$ is something like 8 (any number > 2), is the Q: "if" statement (effectively) a loop instead of a conditional? A1: it is still considered a conditional instead of a loop. A2: yes, the result is kind of loop-like. in fact, some programming A2: languages (like SML, used in CS312), do not have loops. instead, A2: they use conditionals and function call to get the equivalent A2: behavior of loops. A3: actually, the result is rather more complicated than a loop. A3: $n$ is like the loop variable in a $for$ loop. A3: however, in a $for$ loop, you get a linear structure, e.g. A3: $for n = 1:5 ...$ could be visualized like this: n=1 | n=2 | n=3 | n=4 | n=5 A3: but in the case of $fib$, we get a *tree* structure A3: (*tree* is a technical term), e.g. $fib(5)$ looks like this: n=5 / \ / \ / \ / \ n=4 n=3 / \ / \ / \ / \ n=3 n=2 n=2 n=1 / \ / \ / \ n=2 n=1 n=1 n=0 n=1 n=0 / \ n=1 n=0