6.001 Recitation – April 16, 2003

 

RI: Konrad Tollmar

http://www.ai.mikt.edu/~konrad/6001

 

Quiz-2 Review:

 

 

• Environment Model

• Tagged-data

 

• Message-passing

• OOP

• Simple Interpreter

 

 

 

 

 

 

Problem 1 (warm-up)

For each of the following sets of expressions, write down what the last one will return. If it returns an error, or doesn't return at all, say so. You can assume that each set of expressions is evaluated in a fresh Scheme buffer.

 

A

 
(define f (lambda (y) (lambda (x) (set! x (+ x y)) x)))
(define g (f 3))
(g 4)
(g 5)
 

B

 
(define f (lambda (x) (lambda (y) (set! x (+ x y)) x)))
(define g (f 3))
(g 4)
(g 5)
 

C

 
(define g (lambda (x) (list (lambda (x) (+ x 3)) (+ x 5))))
(define h (g 1))
(cons ((car h) 7) (cdr h))
 

D

 
(define x '(a b c))
(define y '(d e f))
(define z (append x y))
(set-car! x 'foo)
(set-car! y 'bar) 
x
y

Problem 2

Draw a box-and-pointer diagram for r, s, x, and y that will result from evaluating the following expressions:

 
(define r (list 'a 'b))
(define s (list 'c 'd))
(define x (append r s))
(define y (append r s))
(set-car! (cdr x) 'e)
(set-cdr! (cdr (cdr y)) (list r))

 

Problem 3  (Tagged data)

 

You are given a list of timecard records for a given week. Some of the timecards are hourly­based:

 

(hourly? !obj?) ­? #t if !obj? is an hourly timecard

(hourly­rate !card?) ­? dollars per hour

(hourly­hours !card?) ­? list of hours worked, e.g. (8 7) or (8 8 8 8 8)

 

and some of the timecards are daily­based:

 

(daily? !obj?) ­? #t if !obj? is a daily timecard

(daily­rate !card?) ­? dollars per day

(daily­days !card?) ­? number of days worked this week

 

A

You need to calculate the total payroll for the week. Consider the procedures below, then complete the pay­for­daily and pay­for­hourly procedures.

 

(define (payroll cards)

  (addup paycheck­amount cards))

 

(define (addup proc LST)

  (if (null? LST)

      0

      (+ (proc (car LST)) (addup proc (cdr LST)))))

 

(define (paycheck­amount card)

  (cond ((hourly? card) (pay­for­hourly card))

           ((daily? card) (pay­for­daily card))

           (else (error ''weird timecard''))))

 

(define (pay­for­daily card)

 

)

 

(define (pay­for­hourly card)

 

)

 

B

Your is in charge of implementing the hourly timecard abstraction. Implement hourly?, make­hourly, hourly­rate and hourly­hours procedures. Show a box­and­pointer diagram for a typical hourly timecard.

 

C

Given the pay­for­hourly procedure was already implemented. Write a total­hours­for­hourly procedure. This procedure takes an hourly timecard, and returns the total number of hours worked during the week of the timecard. Rewrite pay­for­hourly to use your new procedure.

 

 

D

Your maintains the payroll generation software (the payroll and paycheck-amount routines). The company has just added a new kind of employee, and another group has implemented the weekly timecard abstraction. ``Weekly'' employees put in a timecard at the end of any week that they happen to work, and they get paid a weekly rate, e.g. $500 per week. No one records the number of days or hours they show up, just whether or not they worked that week. How must the payroll and paycheck-amount procedures be changed to account for these weekly timecards?

 

E

The company wants to know the total number of hours worked given a set of time cards for the week. Your are responsible for this new time accounting software. Implement total­hours­for­daily that give equivalent hours worked during the week. Define a procedure total­company­hours that, given a list of time­cards, returns the total number of hours worked.

 

 

Problem 4 (Environment Model)

Draw a complete environment diagram with the appropriate value or pointer to the proper environment or procedure object. Assume that the following expressions are evaluated in order.

 
(define (f1 x)
   (let ((a (* x x)))
      (lambda (m) (+ a x m))))
 
(define f2 (f1 3))
(f2 4)
(f2 5)

 

Problem 5 (OOPS)

 

A. Implement the class Machine

 Start and Stop should change the attribute running. Running should return true / false depending if the Machine is running. (define (make-machine ..)  (lambda (msg)   (case msg    ((running) .. )    ((start) .. )    ((stop) .. )    (else (no-method))))) 

 

 

 

 

 

 

 

 

 

 

 

Extend the class Machine with a new subclass Robot. A Robot should be able to change the superclass attribute running with the methods on / off. A Robot should also have a name, and say its name when asked.

 

(define myRobot (make-robot ‘cog))

(ask myRobot ‘press-button)

(ask myRobot ‘name)

 

B. Draw a class diagram that describe the class Robot.

 

C. Implement the Robot class.

 

(define (make-robot ..)   ..     (lambda (msg)      (case msg           .. )))

 

D. Draw an environmental diagram (model) that describes myRobot.