University of Virginia, Department of Computer Science
CS200: Computer Science, Spring 2002

Notes: Friday 25 January 2002
Schedule

Reading Guide
As you read GEB chapter 5, here are some things to think about:
Notes

Scheme Rules of Evaluation

(Repeated from 22 Jan notes, except expanded Rule 4.)

Evaluation Rule 1: Primitives. If the expression is a primitive, it is self-evaluating.

Evaluation Rule 2: Names. If the expression is a name, it evaluates to the value associated with that name.

Evaluation Rule 3: Application. If the expression is an application:
    (a) Evaluate all the subexpressions of the combination (in any order)
    (b) Apply the value of the first subexpression to the values of all the other subexpressions.

Evaluation Rule 4: Special Forms. If the expression is a special form, do something special.

Eval 4-if. If the expression is (if Expression0 Expression1 Expression2) evaluate Expression0. If it evaluates to #f, the value of the if expression is the value of Expression1. Otherwise, the value of the if expression is the value of Expression2.

Eval 4-lambda. Lambda expressions self-evaluate. (Do not do anything until it is applied.)

Eval 4-define. If the expression is (define Name Expression) associate the Expression with Name (for Evaluation Rule 2).

Eval 4-begin. If the expression is (begin Expression0 Expression1 ... Expressionk ) evaluate all the sub-expressions Expression0, Expression1, ... ,Expressionk in order. The value of the begin expression is the value of Expressionk.

Application Rule 1: Primitives. If the procedure to apply is a primitive, just do it.

Application Rule 2: Compound Procedures. If the procedure is a compound procedure, evaluate the body of the procedure with each formal parameter replaced by the corresponding actual argument expression value.

Syntactic Sugar (define (square x) (* x x)) is actually syntactic sugar (and easier way to write something that means exactly the same thing) for:

(define square (lambda (x) (begin (* x x))))
Defining Recursive Procedures
  1. Be optimistic.
    • Assume you can solve it.
    • If you could, how would you solve a bigger problem.
  2. Think of the simplest version of the problem, something you can already solve.
    • This is called the base case.
    • Usually something like solve for 0 or the empty list
  3. Combine them to solve the problem.
(define (find-closest-number-extra 
            goal new-number numbers)
   (if (empty? numbers) 
       new-number ;;; No others, new-number is best
       (if (< (abs (- new-number goal))
                (abs (- (find-closest-number goal numbers)
                    goal)))
           new-number
           (find-closest-number goal numbers))))

(define (find-closest-number goal numbers)
   (find-closest-number-extra goal
     (first numbers)
     (rest numbers)))
This isn't the best way of doing this — think about ways to improve the code.

"Somehow it seems to fill my head with ideas - only I don't exactly know what they are!"
Alice, in Alice and Wonderland by Lewis Carroll after hearing The Jaberwocky.

The best book on programming for the layman is Alice in Wonderland; but that's because it's the best book on anything for the layman.
Alan Perlis, author of SICP Forward and First Turing Award (biggest prize for Computer Science) Winner


CS 655 University of Virginia
Department of Computer Science
CS 200: Computer Science
David Evans
evans@cs.virginia.edu
Using these Materials