c262f97f

c262f97f - CSc 262 Final Examination Thursday 11 December...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
CSc 262 Final Examination Thursday 11 December 1997 8 AM >>>>>>>>>>>>>SUGGESTED ANSWERS<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<, 1. (20 pts) Write a recursive LISP function entryn such that evaluating the expression (entryn n alist) returns the nth entry in the list alist, if it has at least n entries, or nil if it has fewer than n entries. Your function can assume that n is an integer. >>>>>>>>>>>>>>>>>>>>>> (defun entryn (n alist) (cond ( (null alist) nil) ( (lessp n 2) (car alist)) (t (entryn (difference n 1) (cdr alist) ) ) ) >>>>>>>>>>>>>>>>>>>>>> 2. (10 pts) Explain the notion of garbage collection. Explain how the notion of garbage collection applies to LISP and Smalltalk. >>>>>>>>>>>>>>>>>>>>> Garbage collection refers to the freeing up of dynamically allocated memory. In LISP, all lists are represented with linked lists. When they are no longer bound to a variable they are available for garbage collection, which occurs when the system runs out of memory. In Smalltalk, all objects are represented by pointers. When objects are not pointed to they are available for garbage collection, which occurs sporadically. >>>>>>>>>>>>>>>>>>>>>>>> 3. (25 pts) Explain the details of how lists are represented in LISP. Explain how car and cdr are implemented in terms of this representation. Suggest a scheme for implementing "last" and "butlast" in terms of this representation. >>>>>>>>>>>>>>>>>>>>>>>>Lists are implemented with linked lists of nodes, where the "key" of each node is a pointer to the given entry in the list, either an atom or another list, and the "node-pointer" points to the next entry in the linked list. Car is the "key" and Cdr is the "node-pointer" (which points to the rest of the list or NULL if there is no more list). To implement last, return nil for an empty list. Otherwise traverse the list until pointing to the last entry and return its key. To implement butlast return nil if the list is nil or only has one entry. Otherwise, traverse the list to the second last entry, making a copy of the list while traversing. Then set the next node of the second last entry to nil. >>>>>>>>>>>>>>>>>>>>>>>> 4. (25 pts) In Smalltalk I created a class Box, which is a subclass of class Object. Box adds no class variables nor any class methods. Instances of Box have two instance variables, 'corner' and 'extent', which are meant to represent the upper left hand corner of a rectangle (corner) and the dimensions of the rectangle in the x and y directions (extent). Both variables are meant to store points of the form x@y. Instances of Box have two methods, "corner: extent:"
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 5

c262f97f - CSc 262 Final Examination Thursday 11 December...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online