T2 (AddlSlot)
T2 (AddlSlot)
4. Consider the following function and evaluate the expression: (quiz-f '(a b) '(b a)).
(define (quiz-f a b)
(list 'a b '(list a 'b)) )
5. Generate the Huffman tree and find the bit patterns to be encoded for the alphabets A, B, E, S, P, Q
and T. The frequencies of the alphabets are 8, 6, 12, 3, 15, 6 and 23 respectively. Encode the
message TEAPETBET using the constructed tree.
6. Write a procedure check that returns true if the given element x is lesser than the elements in the
given ordered set, else returns false.
7. Using the binary tree abstract data type, write the predicate all-larger? that takes two arguments, a
binary tree of numbers and a single number x, and returns #t if every number in the tree is larger
than the second argument x, else returns #f.
8. The procedure square-list takes a list of numbers as argument and returns a list of the squares of
those numbers.
I/P : (square-list (list 1 2 3 4))
O/P: (1 4 9 16)
Complete the missing terms in the two versions of the procedure written to perform the mentioned
task.
i) (define (square-list items) ii) (define (square-list items)
(if (null? items) nil (map <??> <??>))
(cons <??> <??>)))
Part B (5 x 4 = 20 marks)
9. Assume that the Scheme Interpreter has the functions: accumulate, map, filter, enumerate-tree
and enumerate-interval defined in it. Using the above as conventional interfaces, define the
procedures to perform the following:
i) To find the sum of odd squares of the Fibonacci numbers between 0 and n.
ii) To generate a list with the squares of the even elements in a given tree.
10. Consider the following fold-left procedure
(define (fold-left op initial sequence)
(define (iter result rest)
(if (null? rest) result
(iter (op result (car rest)) (cdr rest))))
(iter initial sequence))
What are the values of
i) (fold-left / 1 (list 1 2 3))
ii) (fold-left list nil (list 1 2 3))
11. Consider the following function.
(define (accumulate f L init-value)
(if (null? L) init-value
(f (car L) (accumulate f (cdr L) init-value)) ) )
What does evaluation of the expression (accumulate cons '(1 2 3) '( )) produce? Show
the work.
12. Define a procedure subsent that takes in a sentence and a parameter i, and
returns a sentence with elements starting from position i to the end. The first
element has i = 0. In other words, (subsent ‘(6 4 2 7 5 8) 3) returns (7 5 8).
13. Write a procedure called intersection-set that returns the list created with the
intersection of the elements present in the two given ordered sets s1 and s2.
For eg., if s1 contains (1 3 5 9) and s2 contains (2 5 6 9), the resultant set is (5
9).
Part C (6 marks)
14. A point can be represented as a pair of numbers: the x coordinate and the y coordinate.
Accordingly, specify a constructor make-point and selectors x-point and y-point that define this
representation. Using the selectors and constructors, write procedure add-points and sub-points that
adds and subtracts the two given points. Write a procedure called print-point that prints the point in
the format (x-point, y-point).
!!!!!!!!!!!!!