T1 (AddlSlot 2016)
T1 (AddlSlot 2016)
√ −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.
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.
~~~~~~~~