What are the four crucial questions to help think about functions consuming a list?
1. What should the function produce in the base case? 2. What should the function do to the first element in a non-empty list? 3. What should calling the function of the rest of the list produce? 4. How should the function combine #2 and #3 to produce the answer for the entire list?
31
New cards
What are the answers to these questions?
1. The length of an empty lsit (the base case) is 0. 2. We don't care what the first value is, just that it exists. There's no need to transform it/ 3. Calling (count-concerts (rest loc)) should produce the length of the rest of the list, an integer 4. We should combine steps 2 and 3 by adding 1 (because the first element exists) to the result of step 3
32
New cards
When is a function recursive?
A function is recursive when the body of the function involves an application of the same function.
33
New cards
What is a condensed trace?
It is space saving tool used in slides that shows the important steps and skips over the trivial details of a full trace.
34
New cards
It is important that our functions always ___
terminate (stop running and produce an answer)
35
New cards
What are the two conditions?
1. it's the base case, which produces 0 and immediately terminates 2. or, it's the recursive case which applies counts-concerts to a shorter list. Each recursive application is to a shorter list, which must eventually become empty and terminate
36
New cards
What is the final form listof-X-template you should use for functions that consume a list?
In a self-referential data definition (like listof-X):
1. at least one clause (and possible more) will use the definition's name to show how to build a "larger" version of the data 2. at least one clause (and possibly more) must not use the definition's name; these are base cases
43
New cards
What is the shape of a self-referential template?
It will be a cond expression with one clause for each clause in the data definition
44
New cards
What do self-referential data definition clauses lead to?
They lead to recursive expressions in the template
45
New cards
Do base case clauses lead to recursion?
No
46
New cards
What is a string?
A sequence of characters in disguise
47
New cards
What function converts a string to an explicit list of character?
string->list
48
New cards
What converts a list of character into a string?
list->string
49
New cards
What is Racket's notation for the character 'a'?
#\a
50
New cards
What is the result of evaluating (string->list "test")?
(cons #\t (cons #\e (cons #\s (cons #\t empty))))
51
New cards
What is a wrapper function?
It is a simple function that "wraps" the main function and takes care of housekeeping details like converting the string to a list.
52
New cards
Wrapper functions:
1. are short and simple 2. always call another function that does much more 3. sets up the appropriate conditions for calling the other function, usually by transforming one or more of its parameters or providing a starting value for one of its arguments