Docsity

Homework 3 Solutions - Program Language | CSC 4101, Study notes of Programming Languages

Toggle sidebar
bg1
Homework 3 Solutions
1) The trick is that the constructor of the class would not be made
public but only private or protected, and that the class maintains
its own instances. E.g.,
class BoolLit extends Node {
private boolean value;
private static ff = null;
private static tt = null;
protected BoolLit(boolean v) { value = v; }
public static BoolLit getFalse() {
if (ff == null)
ff = new BoolLit(false);
return ff;
}
public static BoolLit getTrue() {
if (tt == null)
tt = new BoolLit(true);
return tt;
}
}
In the design patterns literature, they call this a Singleton pattern.
A pure Singleton pattern only maintains a single instance (that's why
it's called Singleton), but as with all design patterns, there can be
variations.
BTW, in order to simplify the implementation of the built-in Scheme
function eq? in Project 2, it would be best to implement BoolLit and
Nil as Singleton patterns.
2) Since we can't modify Widget, the only solution is to define a
forwarding class (aka. adapter, wrapper, or envelope) for Widget.
In the design patterns terminology, they call this an Adapter design
pattern. If class Widget implements some interface WidgetInterface
that is used in the client, then our wrapper could be written
to implement the same interface:
class WWrapper implements WidgetInterface {
Widget obj;
pf3
pf4

Partial preview of the text

Download Homework 3 Solutions - Program Language | CSC 4101 and more Study notes Programming Languages in PDF only on Docsity!

Homework 3 Solutions

  1. The trick is that the constructor of the class would not be made public but only private or protected, and that the class maintains its own instances. E.g., class BoolLit extends Node { private boolean value; private static ff = null; private static tt = null; protected BoolLit(boolean v) { value = v; } public static BoolLit getFalse() { if (ff == null) ff = new BoolLit(false); return ff; } public static BoolLit getTrue() { if (tt == null) tt = new BoolLit(true); return tt; } } In the design patterns literature, they call this a Singleton pattern. A pure Singleton pattern only maintains a single instance (that's why it's called Singleton), but as with all design patterns, there can be variations. BTW, in order to simplify the implementation of the built-in Scheme function eq? in Project 2, it would be best to implement BoolLit and Nil as Singleton patterns.
  2. Since we can't modify Widget, the only solution is to define a forwarding class (aka. adapter, wrapper, or envelope) for Widget. In the design patterns terminology, they call this an Adapter design pattern. If class Widget implements some interface WidgetInterface that is used in the client, then our wrapper could be written to implement the same interface: class WWrapper implements WidgetInterface { Widget obj;

public WWrapper(Widget o) { obj = o; } public int wizbang(int param) { // print param to log file int rv = obj.wizbang(); // print rv to log file return rv; } // If class Widget has other methods that might be called // by our client, we need to provide the appropriate methods // in the adapter as well. public int foo() { return obj.foo(); } } The code that initiated Widget objects would now be written as follows: WidgetInterface p = new WWrapper(new Widget()); Another possibility would be to use inheritance. Class WWrapper would inherit everything from class Widget. Method wizbang() would then be redefined to provide the logging: class WWrapper extends Widget { public WWrapper() { super(); } public int wizbang(int param) { // print param to log file int rv = super.wizbang(); // print rv to log file return rv; } } Widget p = new WWrapper(); If the client refers to the Widget instance through a pointer of type Widget instead of through a pointer of type WidgetInterface, we can only use the inheritance version. Either implementation would qualify as an adapter pattern. For this problem, the version using inheritance would be simpler to write (since we don't need other forwarding methods) and it would be slightly more efficient. The Adapter pattern is a little more general than what's shown in this problem. In many cases, the first implementation would be preferable or more elegant. In this case

The function tail takes this function next out of the cdr of a stream and calls it to produce the rest of the stream. This code is inefficient. Normally, streams would be built such that elements that were accessed before would be cached (or "memoized"). Initially, the stream would be constructed as a cons node containing the first element and the function to compute the rest of the stream, After accessing the second element, the first cons node is updated with set-cdr! so that the stream now contains the first two elements followed by the function that computes the rest. And so on. All we need to change for that is tail: (define (tail stream) (let ((d (cdr stream))) (if (procedure? d) (let ((t (d))) (set-cdr! stream t) t) d))) To make building streams easier, Scheme provides the macro `delay' and the library function force (see Scheme Report, Sections 4.2. and 6.4). In a lazy functional language such as Haskell, all lists really are streams. If Scheme were lazy, we could define even as follows: (define (next m) (cons m (lambda () (+ m 2)))) (define even (next 0))