Sample Problems 2-- CMSC 331 -- Spring 24
Stuff to Study and/or Think About in advance of Test 2
1. What are the types of the following bits of ML (each problem is separate; if there is a dependent function, tell me about it as well):
a) fun newRoman x y = size(x) * size(y);
b) fun newRoman2 (x,y) = size(x) * size(y);
c) fun wild x = jump(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 an int list and an int (n), and returns a new list where every nth item is doubled.
filter([9,8,7,6,5,4,3,2,1],3) => [9,8,14,6,5,8,3,2,2]
4. Write a Ruby function to subtract pairs of ints.
e.g. {7,5} - {2,4} = {5,1}
5. Write a Ruby function which finds the nth item in the 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 LIFO structure with a pop, top, and push operators).
9. Identify languages which are object-free; have traditional scalars 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?