Comp 372 Study Questions Dordal Dec 13, 1999 I apologize for being so late to my office hours Tuesday; I got stuck in traffic. If it will help, I'll be in my office by 7:15 am on Wednesday, the day of the exam, for last-minute questions etc. The following material may be covered on the exam: Polymorphism functional v oo styles ML types and functions Lisp functions (basics of ch5, ch6, 8.2, 10.2, 10.3) Polymorphism in Java CLOS (11.5, 11.6) double-dispatch (ie CLOS generic functions with two parameters) game program The exam will *not* be open book. However, I would not expect you to be able to answer all the *study guide* questions without consulting a reference; if these were used on the exam then some supplemental notes or hints would be supplied on the exam. 1. Consider the following DO loop in lisp: (defun badfact (n) (do ((i 1 (1+ i)) (prod 1 (* i prod))) ((equal i n) prod))) ;; exit condition It doesn't quite work. (a). Explain what (badfact 4) returns. (b). Explain why DO* would fix this. (You may need to look up DO* in the textbook). (c). Fix it while leaving it as a DO (not DO*) loop. NOTE: recall that DO evaluates its variable initializations and updates in *parallel*; DO* evaluates them strictly sequentially. ============================================================== 2. Suppose class Deriv is derived from class Base in C++. (a). Suppose we have the following declarations: Base b; Deriv d; What is the problem with the assignment b=d; (b). Suppose we have the declarations Base ** pp; Deriv ** qq; Explain the hole in the type system that would be introduced if we allowed the assignment pp = qq; HINT: Suppose we have Base * p; Deriv * q; p = & b; q = & d; pp = & p; qq = & q; // the four preceding assignments are all legit And then: pp = qq; // the dangerous one *pp = p; // fundamentally legit To what does p now point? To what does q now point? ============================================================== 3. What are the ML types of the following functions? (a) fun rev(nil, L) = L | rev(h::t, L) = rev(t, h::L); (b) fun foo(nil, nil) = nil | foo(h1::t1, h2::t2) = h1(h2) :: foo(t1, t2); (c) fun c x = fn y => y*x; (d) The function c in (c) above is Curried. Give the type of c(x), and also give an uncurried equivalent uc(x,y). ============================================================== 4. In class I gave the following CLOS example. I assumed that classes Room and Word were defined, and that damControlRoom was a particular Room. (defmethod response((r Room) (v Verb)) ...) ; definition 1 Then, to define response in damControlRoom, I defined (defmethod response ((r (eql damControlRoom)) (v (eql TURN))) ...) ;; definition 2: interprets TURN VALVE in damControlRoom Finally I defined (defmethod response ((r room) (v (eql TURN))) ...) ; definition 3: default interpretation of TURN outside the damcontrolroom. (a). What function is applied if r is damControlRoom but v isn't TURN? (b). As things stand now, what function is applied if r isn't damControlRoom but v *is* TURN? (c). Suppose I define swampRoom, and (defmethod response ((r (eql swampRoom)) (v Verb))...) Suppose swampRoom has no special interpretation for TURN. What would happen if I tried to TURN something in swampRoom? (d). Explain why the problem of (c) might not be expected to arise in practice. ============================================================== 5. Suppose I write a function my_if(a, b, c); a is boolean, and if it evaluates to true then we return b otherwise c. However, I'm not too clever, and so my_if always evaluates all three parameters in full before proceding (that is, a, b, and c are ordinary value parameters). (a). What can go wrong with my_if (x==0, 0, 1/x); (b). what can go wrong with int fact(int n) { return my_if(n==1, 1, n*fact(n-1)); } You should also know about value-result and reference parameters. ============================================================== 6. Lisp lets you attach essentially *anything* to the property list of an atom, with (get x 'key) (setf (get x 'key) 'value) Attempting to retrieve the value for an undefined key simply results in nil. Write a function (calldynamic x y keyname default) that invokes the attached function (on the property list of x with key "keyname") if present, with parameter y, and otherwise calls (default_interpret y) Hint: (funcall FN ARG) calls function FN with argument ARG ============================================================== Other topics: Lisp macros CLOS routine examples Polymorphism: ML v Lisp v C++ templates Polymorphism in Java Parameters