Periodical Test 1 - February 2012: Ax BX C
Periodical Test 1 - February 2012: Ax BX C
Part A (5 x 2 = 10 marks)
1. Convert the following mathematical expression to LISP expression. Assume that the sqrt
procedure is defined already.
2. Evaluate the expression assuming that the default values of x, y and z set as 10, 20 and 30
respectively.
(- (let ((x 1) (y 2) (z (+ x 4))) (+ x y z)) z)
3. Mention the rights and privileges of first class elements.
4. Draw box and pointer notation for (cons (cons (1, cons( a, cons (p, q ))), 5))
5. Construct a lambda procedure to evaluate the expression a x 4 + bx2 +c 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.
Part B (5 x 8 = 40 marks)
Page 1 of 3
7. 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)
(> 3.7 4.4)
(- (if (> 3 4) 7 10) (/ 16 10))
(define b 13)
(define square (lambda (x) (* x x)))
square
(square 13)
(square b)
(square (square (/ b 1.3)))
(define multiply-by-itself square)
(multiply-by-itself b)
(define a b)
(= a b)
(if (= (* b a) (square 13))
(< a b)
(- a b))
(cond ((>= a 2) b)
((< (square b) (multiply-by-itself a)) (/ 1 0))
(else (abs (- (square a) b))))
8. Consider the following procedures. Find the output generated when evaluated with
(p1 3 4). Show how the evaluation occurs using normal order and applicative order
substitution models.
(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)
(p2 b))))
Page 2 of 3
(sum term (next a) next b))))
(define (square x) (* x x)
(define (inc x) (+ x 1))
Use the above definition to execute the following procedure triangle-sum. Define the
order of growth in time for the following functions using Θ notation. (Hint: Use only one
or more of the following: Θ(1), Θ(log n), Θ(n), Θ(n2) and Θ(2n)) Justify your answer.
(define (triangle-sum n)
(sum (lambda (m) (sum (lambda (x) x) 1 inc m))
1 inc n))
Does the procedure generate an iterative process or a recursive process?
10. a) Write a procedure power-close-to that takes two non-zero positive integers (b and n) as
arguments and returns the smallest power of b that is greater than n. That is, it should
return the smallest integer i such that bi > n. You may use the Scheme procedure (expt b
i) which raises b to the power i. (5 marks)
b) Write the output for the following expression (3 marks)
((lambda (b) (* 10 ((lambda (c) (* c b)) b))) ((lambda (e) (+ e 5)) 5))
~~~~~~~~~~
Page 3 of 3