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
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)
(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
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))
You are given a list of timecard records for a given week. Some of the timecards are hourlybased:
(hourly? !obj?) ? #t if !obj? is an hourly timecard
(hourlyrate !card?) ? dollars per hour
(hourlyhours !card?) ? list of hours worked, e.g.
(8 7) or (8 8 8 8 8)
and some of the timecards are dailybased:
(daily? !obj?) ? #t if !obj? is a daily timecard
(dailyrate !card?) ? dollars per day
(dailydays !card?) ? number of days worked this
week
You need to calculate the total payroll for the week. Consider the procedures below, then complete the payfordaily and payforhourly procedures.
(define (payroll cards)
(addup
paycheckamount cards))
(define (addup proc LST)
(if (null?
LST)
0
(+
(proc (car LST)) (addup proc (cdr LST)))))
(define (paycheckamount card)
(cond
((hourly? card) (payforhourly card))
((daily?
card) (payfordaily card))
(else
(error ''weird timecard''))))
(define (payfordaily card)
)
(define (payforhourly card)
)
Your is in charge of implementing the hourly timecard abstraction. Implement hourly?, makehourly, hourlyrate and hourlyhours procedures. Show a boxandpointer diagram for a typical hourly timecard.
Given the payforhourly procedure was already implemented. Write a totalhoursforhourly procedure. This procedure takes an hourly timecard, and returns the total number of hours worked during the week of the timecard. Rewrite payforhourly to use your new procedure.
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?
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 totalhoursfordaily that give equivalent hours worked during the week. Define a procedure totalcompanyhours that, given a list of timecards, returns the total number of hours worked.
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)
A. Implement the class Machine
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.
D. Draw an environmental diagram (model) that describes
myRobot.