In Lisp list processing, which function returns the remainder of the list after removing the first element (i.e., the rest of the list)?

Difficulty: Easy

Correct Answer: cdr

Explanation:


Introduction / Context:
Lisp popularized symbolic AI with elegant list primitives. Two fundamental operations on lists are retrieving the first element and retrieving the remainder of the list after that first element is removed. Properly identifying these functions is essential in understanding recursive list algorithms, tree traversal, and pattern manipulation in AI code.


Given Data / Assumptions:

  • Standard Lisp primitives are in scope.
  • The list is non-empty for the purpose of the basic definitions.


Concept / Approach:
car returns the first element of a list (the head). cdr returns the rest of the list (everything after the head). cons constructs a new list by adding a head element to an existing tail list. last returns the final cons cell or element depending on the dialect. Therefore, the function that returns the list after removing the first element is cdr.


Step-by-Step Solution:
1) Let L = (a b c d).2) car(L) = a; cdr(L) = (b c d).3) cons(x, L) = (x a b c d) for some x.4) last(L) = d (or (d) as a cell), depending on implementation.


Verification / Alternative check:
Run these primitives in any Lisp REPL. Applying cdr repeatedly walks down the list: cdr((a b c)) = (b c), then (c), then ().


Why Other Options Are Wrong:

  • car: Returns the first element, not the rest.
  • cons: Constructs a list; does not return the remainder.
  • last: Retrieves the last element/cell; not the rest.
  • None of the above: Incorrect because cdr is correct.


Common Pitfalls:
Confusing car and cdr; forgetting that cdr returns a list (which may be empty) rather than a single element.


Final Answer:
cdr

More Questions from Artificial Intelligence

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion