T1 (AddlSlot)
T1 (AddlSlot)
Part B (5 x 2 = 10 marks)
6. Construct the following representation as a LISP expression using if and cond.
𝑪 𝒏<2
𝑻𝒆𝒔𝒕(𝒏) = 𝟐 𝑻𝒆𝒔𝒕(𝒏 ) + 𝒃𝒏 𝒏≥𝟐
𝟐
7. Given the following procedures:
(define p1 (lambda (x y) (+ (p2 x y) (p3 x y))));
(define p2 (lambda (z w) (* z w)))
(define p3 (lambda (a b) (+ (p2 a a) (p2 b b))))
What will be the output of (p1 1 2)?
8. Construct a lambda procedure to evaluate the expression 𝒂𝒙𝟒 + 𝒃𝒙𝟐 + 𝐜 with the values of a, b,
c and x as 2, 1, 3 and 4 respectively. Assume that the LISP has a square procedure defined
internally. Make use of this square procedure wherever applicable.
9. Give the output of following expression.
(define a 1)
(let ((x +) (* 3)) (x * *))
(let ( (a (+ 4 7)))
(let ( (b (* a 5))) (+ a b)))
10. Draw the box and pointer notation for (cons (cons (1, cons( a, cons (p, q ))), 5))
Part C (7 x 5 = 35 marks)
11. 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))))
12. 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))
13. Define necessary procedures that compute the sum of the squares of even integers from a
through b (i.e) if the procedure is called as (sum-of-integers 3 9) the output generated should
be 42 + 62 + 82 = 116.
14. 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].
15. 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))))))
16. 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))) ) )
17. 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.
~~~~~~~~~~~