Docsity

Principles of Programming Languages Quiz II - Prof. Krishnamurthy Subramani, Quizzes of Computer Science

Toggle sidebar
bg1
Principles of Programming Languages - Quiz II
K. Subramani
LDCSEE,
West Virginia University,
Morgantown, WV
1 Instructions
1. The quiz is to be turned in by 12 : 00 noon.
2. Each question is worth 3points.
3. Attempt as many problems as you can. You will be given partial credit, as per the policy discussed in class.
2 Problems
1. Type-Checking:
Consider the following ML function definition:
> fun thrice f x = f(f(f(x)));
Use the Hindley-Milner type-checking algorithm (or any logical procedure) to deduce the type of thrice(). You are
required to determine the most general type.
2. Expressions and Statements:
(i) Explain the difference(s) between the if-expression and if-statement in C.
(ii) Given the semantics of the assignment statement in C, will the following fragment of code work? Can it be
made to work? Justify your answer.
(a > b)? (a=3): (b=4);
3. Procedures and Environments:
Consider the following Cprogram:
int i;
int b[5];
void q (int x)
{
i++;
x++;
}
1
pf2

Partial preview of the text

Download Principles of Programming Languages Quiz II - Prof. Krishnamurthy Subramani and more Quizzes Computer Science in PDF only on Docsity!

Principles of Programming Languages - Quiz II

K. Subramani

LDCSEE,

West Virginia University,

Morgantown, WV

{[email protected]}

1 Instructions

  1. The quiz is to be turned in by 12 : 00 noon.
  2. Each question is worth 3 points.
  3. Attempt as many problems as you can. You will be given partial credit, as per the policy discussed in class.

2 Problems

  1. Type-Checking: Consider the following ML function definition:

fun thrice f x = f(f(f(x)));

Use the Hindley-Milner type-checking algorithm (or any logical procedure) to deduce the type of thrice(). You are required to determine the most general type.

  1. Expressions and Statements:

(i) Explain the difference(s) between the if-expression and if-statement in C. (ii) Given the semantics of the assignment statement in C, will the following fragment of code work? Can it be made to work? Justify your answer. (a > b)? (a=3): (b=4);

  1. Procedures and Environments: Consider the following C program:

int i; int b[5];

void q (int x) { i++; x++; }

main() { i=1; b[1]=3; b[2]=4; q(b[i]); printf(โ€˜โ€˜%d \nโ€™โ€™,b[i]); }

What value will be printed assuming that C uses the following parameter passing mechanisms: (i) Pass by value, (ii) Pass by value-result, (iii) Pass by name.

  1. Scheme programming: Write a function in SCHEME that takes as input two sorted integer lists L and M and returns a list obtained by merging L and M. You may assume that the lists are sorted in ascending order.
  2. ML programming:

(i) Describe how you would declare a type for Binary Search Trees on integers in ML. (ii) Write a function named PRE-TRAVERSE(), which takes as input a Binary Search Tree of the form described above and outputs the list of elements obtained by a pre-order traversal of this tree.