[an error occurred while processing this directive]

Class 4 Notes

Lecture Slides, Suitable For Printing

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
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
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:
[an error occurred while processing this directive]