CISC 280 review sheet for final exam, 1pm, May 20, 2002

Exam covers entire course with emphasis on chapter 3 and section 4.1

Exam will be similar in structure to the midterm exams and see review guides for midterm exams for sample questions on chapters 1 and 2.

See the web page for summary of lecture topics and various handouts. Covered reading is everything through section 4.1 excluding: 2.3.3,    2.5.3,    3.3.4,    3.3.5.

Outline

Section 4.1 - Metacircular Evaluator:
Writing eval in Scheme itself, we learn several things.
  1. It is surprisingly short. The core procedures of Scheme need not be extensive programs.
  2. From this implementation we can understand how the environment model really works.
  3. Such implementations (of a language in itself) can serve practical purposes. This can be the most effective way to build more efficient interpreters or compilers, and it can be a way to introduce new special forms into the language.
Exercise 4.1
Chapter 3 - Mutable Data
The bank account example. If a parent procedure (make-account) returns a child procedure (such as dispatch) defined within it, then the parameters (such as balance) of the parent procedure are part of the environment of the child procedure. They can be changed by the child procedure thru the use of set! and are private mutable data objects.

Use of set-car! and set-cdr! in implementing queues and stacks. When using set-car! and set-cdr!, one must be aware that the pair being changed may be a part of several lists your program is manipulating. Care must be taken that such changes do not have unwanted effects on other lists. For example:

  1. (define x '(1 2 3))
    (define y (cons x x))
    (set-car! (cdr x) 22)
    
    Now what is x and what is y? Use box and arrow diagrams to understand what happens.

  2. Define a procedure (make-count-down n). It returns a procedure which decrements a local variable (initial value is n). It returns false, #f, if the variable hasn't reached zero, returns true, #t, if it has. For example, after you have defined make-count-down, I could have the following:
    > (define counter (make-count-down 3))
    --> #unspecified
    > (define (watcher) (if (counter) 'Ka-boom! 'not-yet))
    --> #unspecified
    > (watcher)
    --> not-yet
    > (watcher)
    --> not-yet
    > (watcher)
    --> not-yet
    > (watcher)
    --> Ka-boom!
    
  3. Some more good exercises:
       set! and local state: 3.1, 3.2, 3.3, 3.4,
       set-car! and set-cdr!: 3.12, 3.13, 3.14,
       queues: 3.22, 3.23

The special form delay and procedure force allow you to have a lazy evaluation strategy. In particular they can be used to have streams with

(cons-stream a s); a special form
the-empty-stream
(empty-stream? s)
(stream-car s)
(stream-cdr s)
Streams allow you to conceptually create arbitrarily large sequences, even infinite ones, without wasted computation of producing values in the sequences unless they are actually used. Good stream exercises are 3.53, 3.55
  1. Define an infinite stream.
  2. Given
    (define (odd-part n) (if (odd? n) n (odd-part (/ n 2))))
    (define (3n+1 n) (odd-part (+ 1 (* 3 n))))
    (define (triple-3n+1 n) (3n+1 (3n+1 (3n+1 n))))
    
    Define a procedure which returns the stream of positive integers k for which (triple-3n+1 k) is greater than k.

Data structure manipulation functions --
   constructors / selectors / recognizers / mutators.

Multiple representations of similar data:

  1. Using a function that is dispatches on object type for a function oriented approach
  2. Letting the objects be dispatch procedures for object oriented programming.
  3. Using a 2 key table for a function/object neutral implementation.
Chapter 2 - Data abstraction

Pairs:   cons/ car cdr / pair? null?

Uses of pairs:
   abstraction barriers - levels from basic scheme up to application functions

Sequences (lists):    list append reverse / nth length / list?

Symbols and quote '

Symbolic differentition: Expressions are naturally processed using tree recursion Simplification is an issue that Make-sum, etc can handle.

rational numbers, interval arithmetic:

List processing:    (map f L) (filter p? L) (accumulate binop unit L) More list processing:    (flatten L) (count-pairs L) (map f L1 L2)

Multiple representations    Manifest type    Data directed programming

Some good exercises from chapter 2: 2.18, 2.21, 2.22, 2.23, 2.24

I - Procedural abstraction

Control -    Repetition (recursion, iteration),
   Selection (cond, if, and, or),
   Sequence - args evaluated before body. sequence in cond clauses, in lambda bodies.

Computing with numbers.
   Use of + - * / quotient remainder
   Use of zero? even? odd? < > = <= >=

Big O.

Procedures as parameters, as returned values.

Exercise: Define a procedure (compose f g). It takes two one-argument procedures f and g and returns a one-argument procedure which for any argument x returns f of g of x. For example, compose could be used thus:

-> (define biggie (compose factorial square))
-> (biggie 2)
Value:  24
-> (biggie 10)
Value:  933262154439441526816992388562667004907159682643816214685929638952175999
93229915608941463976156518286253697920827223758251185210916864000000000000000000
000000 ;; 100 factorial

The final exam will be similar in structure to the preceeding exams. You can expect some questions similar to (maybe even identical to) questions in previous tests, in homeworks, in the text.