Fall 2011 Additional Problem for Chapter 4
Fall 2011 Additional Problem for Chapter 4
Fall 2011 Additional Problem for Chapter 4
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Name two examples from C++ where a variable is “live” (in use) but not in scope.Extra <strong>Problem</strong> <strong>for</strong> <strong>Chapter</strong> 11Following the example of intsfrom in streamtest.sml, write a function, fib ( ), which creates astream of all the Fibonacci numbers. Fibonacci numbers are defined as follows:f 0 = 0f 1 = 1f n = f n-1 + f n-2 , where n > 1You will need to save state inside of fib somewhere (hint: use a local function defined inside if fibthat computes the next Cons). You should be able to obtain the following output:- val f = fib ( );val f = Cons (0,fn) : int stream- printStrm 10 f;0112358132134val it = () : unitProgram <strong>for</strong> <strong>Chapter</strong> 11Write functions <strong>for</strong> processing a semimap – a combination of a map and a set. A semimap is apolymorphic list of two possible items: (key,value) pairs or singleton keys. Keys must be unique.The following functions are required:pairCount – returns the number of pairs in the semimapsingCount – returns the number of singleton entries in the semimaphasKey – returns whether a key exists in a semimapgetItem – returns the pair or singleton containing a given key as an option, to allow <strong>for</strong> NONEgetValue – returns the value <strong>for</strong> a key as an option if it is in a pair, or NONEgetKeys – returns all the keys in the semimap as a listgetValues – returns all the values from the pairs in the semimap as a list
insertItem – returns a new semimap with a new pair or singleton inserted, unless the key is alreadyin use (in which case return the list unchanged; order is not important)removeItem – returns a new semimap with the pair or singleton with a given key removed, if it wasthereAll functions with more than one parameter should be curried. Here is a sample execution:(* ML Statements *)val stuff = [(P (1,"one")), S 2];pairCount stuff;singCount stuff;getItem stuff 1;getItem stuff 10;getValue stuff 1;getValue stuff 10;getKeys stuff;getValues stuff;removeItem stuff 1;insertItem stuff (P (3,"three"));stuff;(* Output *)val stuff = [P (1,"one"),S 2] : (int,string) semipair listval it = 1 : intval it = 1 : intval it = SOME (P (1,"one")) : (int,string) semipair optionval it = NONE : (int,string) semipair optionval it = SOME "one" : string optionval it = NONE : string optionval it = [2,1] : int listval it = ["one"] : string listval it = [S 2] : (int,string) semipair listval it = [P (3,"three"),P (1,"one"),S 2] : (int,string) semipair listval it = [P (1,"one"),S 2] : (int,string) semipair list<strong>Problem</strong> <strong>for</strong> <strong>Chapter</strong> 12As mentioned in class and in the book, languages that do not support recursion, like classicalFortran, can statically allocate activation records instead of placing them on the stack. Even <strong>for</strong> suchlanguages, why might it be advantageous to allocate activation records on the stack in suchlanguages?<strong>Problem</strong> <strong>for</strong> <strong>Chapter</strong> 16Consider the example class hierarchies (the A, V, and Y hierarchies) on the last slide <strong>for</strong> <strong>Chapter</strong>16. Rework the problem, using the method of creating an ordered list of “most derived functions”,with the following change: the order of the parameters in the function f is reversed (i.e., the Yargument comes first, then the V argument, then the A argument). Then rewrite multimeth.cpp tocorrectly handle the new version of f (you may use C# or Java instead, if you like). Turn in the workyou did in developing the ordered list as well as working code and the associated printed output.