Corrections, FAQs, and Hints for Assignment 5

CSc 372, Fall 2006

Last Update: November 1, 2006, 3:02pm


Correction

 

1.         The following is added to Problem 7, X.java:

 

Keep track of the time you spend on this problem and include it as a comment in your solution. Without the time, the problem is worth nothing. If you work on it for a while but eventually give up, you can still get the points by simply reporting how much time you spent on it.

 

FAQs

 

Q1:      The HTML produced by mtimes.rb, as provided in the mtimes.out.* files, does not appear to be well-formed. Should we match it or should we produce well-formed output?

A:        That shows my ignorance of HTML. I did test the results with Firefox, IE, and Opera and didn't have any problems so I'm inclined to leave it as-is unless a compelling reason to change it arises.

 

Executive summary: Match my output, albeit goofy. In the spirit of Henry Ford's thinking about car colors, "You can have any color you want as long it's black.", you can produce any HTML you want as long as it matches mine.

 

Q2:      In the Park Place times for Gridiron Gang there's a comma missing: (12:00) 1:30. Will you be fixing that?

A:        The truth is "Oops!" but let's imagine that the provider of that information has some buggy software. Your code should be able to deal with that glitch, and any others you may find but remember that the input data is finalized. You have to deal with mtimes.[01] as they stand but you don't have to worry about anything else being thrown at you.

 

Q3:      Why does XString#each return that range?

A:        That's an artifact of my implementation that slipped through into the write-up. It would be better to follow existing examples of each, like Array#each, and return the string. But, I overlooked that. Because this artifact is long-standing I won't correct it now but you can easily match it by simply having 0...size as the return value for your implementation of each.

 

Q4:      My XString works fine on simple cases but it blows up or goes into a loop when I try to make a nested string, like MirrorString(ReplString(MirrorString....

A:        The problem may be in your inspect routines. Here's a common error:

 

   def inspect

       "MS(#{@s})"

   end

 

That string interpolation is equivalent to this: "MS(" + @s + ")". The concatenation "MS(" + @s requires the value of @s to be a String and to_s is used to produce that String. If @s is a big ReplString then you've got a problem! The solution is simple:

 

       "MS(#{@s.inspect})"

 

If @s is an XString then inspect will produce RS(...) or MS(...). If it's a String then it will produce something like "abc".

 

Note that Ruby is a little richer than Java: Ruby makes a distinction between inspect and to_s but Java's toString() is one-size-fits-all. There's actually a third choice in Ruby, to_str, but that's another story.

 

Q5:      For label, I don't understand what you're up to with this:

 

    def label(x, sequence = [0] , h = {})

 

A:        h and sequence are essentially "accumulator parameters", a technique we often used in ML to compensate for the lack of variables, although I see that I never mentioned that term in the slides.. The state of the computation is represented by an array sequence number and a hash whose key/value pairs represent array labels and object_ids for arrays. sequence is a one-element array so that an increment, sequence[0] += 1, is visible to the caller. It's a Ruby analog for "int *sequence" in C. In ML we'd perhaps return the sequence and the hash as a tuple.

 

It's good to understand the above but a simpler solution works. Here's how it starts:

 

def label x

$sequence = 0

$h = {}

label1 x

end

 

    def label1(x)

return x.inspect if !x.is_a? Array

...

 

 

The above solution is not thread-safe because overlapping calls to label in multiple threads would clobber the globals. Ruby does support threads but we're not worrying about them in our 372 world so the above solution, albeit low-tech, works.


Typos

 

1.         About the middle of page 3 of Part 1: It's very simple but [if] you have any trouble understanding the structure of it, please let us know.