Computer Science- Recursion, Iteration, and Lists (Test 2) Flashcards
(20 cards)
stepper
helps identify your mistakes
trace diagram
- traces steps of function
- start with the function call
recursive
- uses itself to define itself
- one that calls itself
base case (exit case)
- simple case value we know how to evaluate without a recursive call
- stops the recursion
recursive reduction
- function calls a simpler version of itself/ version closer to “the answer”
- must eventually read the base case
Developing A Recursive Procedure
- write down simple cases and expect answers
- write a case that is slightly more complex than base case
- how can i get this case to above case and what must be done to the reduction?
To Approximate the Square Root of n
- make a guess
- divide n by the guess
- add the result to your guess
- take half of the sum
E(psilon)
identifies acceptable region
stack memory
- each slot is removed, one by one, from top to bottom
- bottom is the function being evaluated and the top is the base case
- THE BOTTOM IS THE DEFERRED (DELAYED) OPERATION
Hallmarks of Iteration
- no deferred/delayed operation
- a fixed # of state variables whose values precisely specify the current state
- fixed transition rule
- an (optional) end test: specify conditions under which the process should terminate
fixed transition rule (iteration)
describes how the state variables are updated as the process moves from state to state
Writing Recursive Procedures That Will Generate An Iterative Process
- generally needs a helper function
- generally helper fxn needs 1 more input than wrapper
- devise transition rule, then translate to scheme
proof
an irrefutable argument
LISP
- list processing
- scheme is a dialect of LISP
2 rules of scheme
- everything is a list
- everything is a function
List in Scheme
- is comprised of the empty list and 0 or more elements
- a single quote tells scheme: “do not try to interpret what follows as a function”
- comprised of 2 parts: CAR and CDR
CAR
- first element of list
- outputs can be either: LIST, ATOM, OR ERROR
CDR
- everything expect first element
- outputs can be either: LIST OR ERROR (cdr ‘( ))
Cons Cell Diagram
- left compartment: car part of each block, can hold ATOM AND ORIGIN OF ARROW
- right compartment: cdr part, can hold ORIGIN OF ARROW
Writing List-Processing Functions
- (null? L) is usually the base case
- a list decomposes into CAR & CDR
- examine CAR to determine possible case THEN accordigly, make recursive call on CDR