[an error occurred while processing this directive]
cs150: Notes 5
Assignments Due
- (extended from 29 January) Before Monday, 5 February: Read GEB, Little Harmonic Labyrinth and GEB, Chapter 5
- Friday, 2 February (beginning of class): Problem Set 2. (Note: Problem
Set 2 will be accepted without penalty or any permission required until
the beginning of class on Monday, 5 February, as long as you promise to
still finish reading GEB Chapter 5 before Monday's class.)
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.
[an error occurred while processing this directive]