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

Info icon This 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.
Image of page 1

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern