Sample Problems 2-- CMSC 331 -- Spring 17

Stuff to Study and/or Think About in advance of Test 2

	
1.  What are the types of the following bits of ML:

	a) fun diff x y = size(x) - size(y); 
              
        b) fun diff2 (x,y) = size(x) - size(y);

	c) fun wild x = size(x) |
               wild 0 = 1; 

	d) fun apply f [] = [] |
               apply f (a::b) = (f a)::(apply f b);

	e) fun zipper f a b = f(a,b);


2. Using pseudo-code, give examples of two ways in which param passing can lead to aliases.

3. Write an ML function which takes a list and an int (n), and returns a new list containing only every nth item from the original list.

   filter([9,8,7,6,5,4,3,2,1],3) => [7,4,1]

   filter(["a","b","c","d","e","f"],2) => ["b","d","f"]

4. Write an Ruby function to subtract pairs of ints.
   e.g. {7,5} - {2,4} = {5,1}

5. Write an Ruby function to add up all of the ints in an int list.

6. What mode of parameter passing best supports a side-effect free language? 

7. Write a grammar  which generates all the sentences where it starts with n copies of "a", followed by exactly n copies of "b", where n>0 and n must be even.

   aabb, aaaabbbb   are in the language.
   ab, aaabbb  are not in the language.

8. Write a Ruby class which implements stacks of ints (i.e. a FIFO structure with a pop, top, and push operators).

9. Identify languages which are object-free; have traditional scalars plus objects; have full traditional type system plus objects; only have objects.

10. Are all sub-classes also sub-types?  Why is this the case?

11. What language features is lost if space allocation is purely static?

12.   fun tester (int x, int y) { };

      z = tester{y=3; x=9};

what sort of parameter matching is going on here?

13, Pass-by-reference is always more efficient.  Discuss.

14.  fun swap (in x, in y) { 
         temp = x;
         x = y;
         y = temp;
     }

     A = "first"; 
     B = "last;
     swap(A,B);
     print A;

what is the output of the above code?