[an error occurred while processing this directive]
Class 4 Notes
Notes and Questions
Is it better to solve problems by thinking about what we need to do to
solve the problem (procedures) or by thinking about what we
need to represent to solve the problem (data)?
What does cons do?
What do car and cdr do?
What is the value of (car (cdr (cons 1 (cons 2 (cons 3 null)))))?
Why do we need the special list null?
Are there any data structures that cannot be built using just cons?
How could we define cons, car and cdr if Scheme did
not have them as primitives?
A list is either (1) null or (2) a pair where the second
part of the pair is a list. To define procedures on lists, we need to
handle both cases: what do we do when the list is null and what
do we do when the list is a pair.
Define length a procedure that takes a list as its
operand and evaluates to the number of elements in that list.
Procedures Practice
Defining Procedures
- First, make sure you know what the procedure is intended to do.
You should know what the inputs are (and what types of values they are),
and what type of value the output is (e.g., a procedure, a number, a
list). You should also have some example inputs and outputs in mind.
- Before worrying about the code, think in English how you will solve
the problem.
- Define the procedure.
- Test your procedure with the example inputs and outputs. Think
about any tricky cases that you should also test.
1. Define a procedure that takes three inputs, the first two inputs are
procedures, and the third input is a number. The output of the
procedure should be the maximum value produced by applying either the
first or second input procedure to the input number.
Recursive Definitions
- Be optimistic!
- Base case — think of the simplest version of the
problem that you already know the answer to. For problems where the
input is a number, this is often when the input is 0. For problems
where the input is a list, this is nearly always when the list is the
empty list (null).
- Recursive case — think of how you would solve an
instance of the problem using a slightly smaller version of that problem
instance. For problems where the input is a number, this often involves
subtracting 1. For problems where the input is a list, this almost
always involves the cdr of the list.
- Combine the base case and recursive case.
Here is the find-maximum procedure from Chapter 4:
(define (find-maximum f low high)
(if (= low high)
(f low)
(max (f low)
(find-maximum f (+ low 1) high)))))
2. (Exercise 4.8) The find-maximum procedure we defined
evaluates to the maximum value of the input function in the range, but
does not provide the input value that produces that maximum output
value. Define a procedure that finds the input in the range that
produces the maximum output value.
3. (Exercise 4.9) Define a find-area procedure that
takes as input a function f, a low range value low, a
high range value high, and an increment inc, and
produces as output an estimate for the area under the curve produced by
the function f between low and high using the
inc value to determine how many points to evaluate.
Two especially useful DrScheme commands:
- [Tab] — Press the [Tab] key for DrScheme to indent your code
structurally. All your code should be divided into logical lines that
fit in the page with, and indented to show its structure clearly. To
indent all your definitions, use Ctrl-I.
- Esc-P — In the interactions window, use
Esc-P to retrieve your last command. To go further back, press
Esc-P again. This saves typing when you are trying different
things.
[an error occurred while processing this directive]