0% found this document useful (0 votes)
27 views2 pages

T1 (AddlSlot 2016)

Uploaded by

Supriya M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views2 pages

T1 (AddlSlot 2016)

Uploaded by

Supriya M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Amrita Vishwa Vidyapeetham

Amrita School of Engineering, Bengaluru Campus


Department of Computer Science & Engineering
Periodical Test 1 - September 2016 (Additional Slot)
Course Name & Code: Structure and Interpretation of Computer Programs (CSE221)
Date & Time: 17/09/2016 Duration: 2 Hrs.
Branch& Semester: CSE – AS Max. Marks: 50
Answer all the questions:
Part A (5 x 3 = 15 marks)
1. Write the output for the following LISP expressions
i) (define (if a b c) (+ a b c))
(if 20 30 40)
ii) (* (+ (* 2 4) ( - (/ 6 2) 4)) 6)
2. Convert the following mathematical expressions to LISP expression. Assume that the sqrt
procedure is present internally.

√ −b+ √ b −4 ac
2
i) ii) s(s−a)(s−b)(s−c)
2 a /bc
3. Given the following procedures:
(define P (lambda (x y) (+ (Q x y) (Q x y))))
(define Q (lambda (z w) (* z w)))
What will be the output of (P 1 2)?
4. Evaluate the following let expressions.
(define a 1)
(let ((x +) (* 3)) (x * *))
(let ( (a (+ 4 7)))
(let ( (b (* a 5))) (+ a b)))
5. Construct a lambda procedure to evaluate the expression bx 2 +c with the values of b, c and x as
1, 3 and 4 respectively. Assume that the LISP has a square procedure defined internally.

Part B (7 x 5 = 35 marks)
6. Predict the output the interpreter will print in response to evaluation of each of the following
expressions. Assume that the sequence is evaluated in the order in which it is presented here.
(- 8 9) (define multiply-by-itself square)
(> 3.7 4.4) (multiply-by-itself b)
(- (if (> 3 4) 7 10) (/ 16 10)) (define a b)
(define b 3) (= a b)
(define square (lambda (x) (* x x))) (if (= (* b a) (square 13)) (< a b) (- a b))
square (cond ((>= a 2) b)
(square 13) ((< (square b) (multiply-by-itself a)) (/ 1 0))
(square b) (else (abs (- (square a) b))))
7. How many times is * called in the following LISP code using Applicative and Normal order
substitution models? Show the work completely.
(define (square x) (* x x))
(define (foo x y) (+ x (* y y)))
(foo (* 2 2) (square 3))
8. Convert the following representation to LISP procedure and show the process generated when
calling the procedure with (Comp 6). Write the final output also.

Comp (n)= {Comp ( n−1)∗Compn( n−2


n<4
) +Comp (n−3)n ≥ 4 }
9. Define necessary procedures that compute the Least Common Multiple (LCM) of given two
numbers. LCM of given two numbers a and b can be calculated as ab / GCD(a,b) where
GCD(a,b) is the Greatest Common Divisor of a and b. [Hint: GCD(a,b) is a function which calls
itself with parameters b and r where r is the remainder when a is divided by b].
10. Given the following procedure repeated, what will be the value of the expression ((repeated
square 2) 5) assuming that the procedure square computes the square of a given number?
(define (repeated p n)
(cond ( (= n 0) (lambda (x) x))
( (= n 1) p)
( else (lambda (x) (p ((repeated p (- n 1)) x))))))
11. Determine the order of growth in time and space for the following functions using Θ notation.
Also identify whether they generate iterative or recursive process. [Hint: Use one of the
following for your answers: Θ (1), Θ (logn), Θ (n), Θ (n2), Θ (2n)].
i) (define (square n) ii) (define (number-of-bits-in n)
(cond ((= n 0) 0) (if (< n 2) 1
((even? n) (* (square (quotient n 2)) 4)) (+ 1 (number-of-bits-in (/ n 2)))))
(else (+ (square (- n 1)) (- (+ n n) 1))) ) )
12. Two functions appear below, one of which returns the next prime number
greater than its argument, the other of which returns the next leap year
following its argument.
i) (define (next-prime k) ii) (define (next-leap-year year)
(if (prime? (+ k 1)) (if (leap-year? (+ year 1))
(+ k 1) (+ year 1)
(next-prime (+ k 1)) ) ) (next-leap-year (+ year 1)) ) )

Write a function that generalizes the two functions above, and show how to
call it to produce the effect of next-prime and next-leap-year.
~~~~~~~~

You might also like