Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Type T Array (1..10) of Integer S T A: T B: T C: S D: Array (1..10) of Integer

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Birla Institute of Technology and Science

Second Semester 2013-14, Comprehensive Exam (Open Book)


Principle of Programming Languages
CS F 301
Date: 15/05/2014 Time : 3 PM - 6 PM Total Marks: 100 / 200
Q1. Consider the conditional expression construct in C like programming language whose syntax is given below.

<Condition>? <trueExpression> : <falseExpression>.

Compiler can evaluate this expression in either lazy order (condition is evaluated before expression) or eager order
(condition and expression are evaluated together). Most programming languages follow lazy order evaluation.
Explain with example what may be the problem with eager order evaluation?. 4M

Q2. Languages such as Java, C++ and C# provides explicit parametric polymorphism and Languages such as ML,
Smalltalk, and some scripting languages support implicit parametric polymorphism i.e. functions naturally accept
arguments of any type that supports the operations applied to them by the function. Discuss the strengths and
weakness of explicit and implicit parametric polymorphism. 4M

Q3. Meaning of the keywords value, ref, result, and name when associated with parameters is given below
value: pass by value, ref: pass by reference, result: pass by value result and name: pass by name. 10M
void function 1(ref a, ref b, result c, result d) int function2(int name i, int value start, int value stop, int name
{ expr) { int
a= a+b+c+d; ret=0; i=start;
b= a+b+c+d; while(i!=stop){ ret =ret + expr; i=i+1) } return(ret);
c= a+b+c+d; }
d= a+b+c+d; void main( ) { int x =2,y; y= function2(x,0,5,x,x); }
a= a+b+c+d; a) For each iteration of the loop, write values of i, ret, and expr.
} i ret expr
void main( ) Before Loop:
{ Iteration 0:
int x =1;y=2; Iteration 1:
function(x,y,x,x); Iteration 2:
} Iteration 3:
What is the value of x and y when main( ) Iteration 4:
completes its execution? b) What is the value of x and y when main( ) completes its
execution?
Assume that the arguments are copied back
from left to right.
Q4. Consider the scheme like program given below. Give output for different combinations of variable binding and
binding of functions arguments. 8M
(define x 0)
(define y 0)
(define (f z) (display ( + z y))
(define (g f) (let ((y 10)) (f x)))
(define (h) (let ((x 100)) (g f)))
(h)
Variable binding Function argument binding Output
Static Dynamic Shallow Deep
☺ ☺
☺ ☺
☺ ☺
☺ ☺
Q5. In the following code, which of the variables will a compiler consider to have compatible types under structural
equivalence? Under strict name equivalence? Under loose name equivalence? 3M
type T = array [1..10] of integer
S=T
A:T
B:T
C:S
D : array [1..10] of integer
Q6.

Q6a.The Let construct is not a fundamental scheme primitive. Give a lambda expression that has the same effect as
the following Let expression: (let ((v1 e1) (v2 e2)) x) 3M

Q6b. What is the output of the following scheme programs 4M

(let ((x 5)) (let ((x 5))


(let ((x 6) (f (lambda (y z) (* y (- z x))))) (let* ((x 6) (f (lambda (y z) (* y (- z x)))))
(f x 8))) (f x 8)))
(define f (define f
(lambda (a b) (lambda (a b)
(let ((a b) (b a)) (list a b)) (let* ((a b) (b a)) (list a b))
)) ))
Q7. Consider two λ-calculus expression S and K. 10M
S = λx. λy. λz.(xz)(yz) K= λ x. λ y.x Prove SKK is an identity function

Q8. Perform β-reduction using lazy and eager evaluation order on the λ-expression as much as possible. 10M

(λabc. abc)( λxy. x)(( λf. fff)( λg. g))(( λwv. wv)( λt. t)( λu. u))

Q8. Complete the following program which convert prefix expression to infix expression 5M

(define infix
(lambda (expr)
(if (list? expr)
(cond
((= 2 (length expr)) (list (car expr) (infix (Q8a))))
((= 3 (length expr))
(list (infix (cadr expr)) (car expr) (infix (Q8b)))
) (else expr)
)
expr Sample Input: (infix '(+ (+ (^ (- x) 2) (* 2 x)) 1))
) Sample Output: '((((- x) ^ 2) + (2 * x)) + 1)
)
)

Q9. Define a scheme function pairwise that accepts a function f and returns a function that accepts an even number
of arguments, applies f to successive pairs of arguments, and returns the list of results; an empty list should be
returned if the number of arguments is odd. 7M
((pairwise +) 1 2 3 4 5 6) produces (3 7 11)

Q10. Give DFA to recognize the binary string


a. With at least one 1 in each sub-string with length 3. 4M
b. With second and last letter is 1. 6M
Q11. Consider the following grammar 5M
Bool--> Bool and Reln / Bool or Reln / Reln
Reln --> Integer < Integer / Integer = Integer / TF
TF--> true / false
a . Give parse tree for the string true or true and false. b. Eliminate Left recursion from the grammar.
c. Eliminate Left factoring from the grammar.

Q12. What strings are represented by the following regular expression? 2M


(xUy)* (xUy) (xUy) (xUy) (xUy) (xUy) (xUy) x (xUy) (xUy) (xUy) (xUy)
Q13. Consider the following function:

procedure bar( x : int, ref y : int )


procedure helper( a : int, ref b : int )
function helper_two( c : int ) : int
begin
var ret : int;
% Arbitrary code here
ret := helper_three( a, b * c );------------------------> Position1
return ret
end
begin
% Arbitrary code here
temp := a * 2; ----------------------------> Position2
b := helper_two( b )
end;
function helper_three( d : int, e : int ) : int
begin
var ret : int;
% Arbitrary code here
return ret
Show the contents of the stack at this point
end
begin
var temp : int;
% Arbitrary code here
helper( x, y )
end

Suppose the following code is located in the main program:


var a : int;
var b : int;
% Arbitrary code here
bar( a, b )

a. Give the run time stack right before the helper_three returns. Show static and dynamic link. 5M
b. Give the reference pair (chain offset, local offset) for each variables at Position 1and Position 2 in the functions.
3M
Q14. We had written a recursive version of sum-list to perform summation of all elements in the list. Was that tail
recursive? if no, modify the function to be tail recursive. 5M
Discuss the strength and weakness of tail-recursive and not tail-recursive functions. 2M

You might also like