In Common Lisp, what does the predicate function (endp <list>) return when <list> is empty, and what is its role compared with null?

Difficulty: Easy

Correct Answer: It returns t if the list is empty

Explanation:


Introduction / Context:
Lisp programmers frequently test for the end of a list while traversing structures. Common Lisp provides the predicate endp specifically for this purpose. Understanding how endp behaves clarifies robust list-processing patterns and prevents errors when handling dotted lists or non-list values.


Given Data / Assumptions:

  • endp is applied to a value expected to be a list.
  • We interpret t as the canonical true value in Lisp.
  • We contrast endp with other operations such as length and list copying.


Concept / Approach:
endp returns t when its argument is the empty list, and nil otherwise. It is intended for detecting the termination condition during recursion or iteration over lists. It does not copy lists, nor does it compute lengths. While null also tests for the empty list, endp is semantically tied to proper list termination and can signal an error for arguments that are not lists, which encourages cleaner list-processing code.


Step-by-Step Solution:

Identify desired behavior: determine whether a list is empty.Recall definition: endp returns t if argument is the empty list.Exclude unrelated operations like copying and length calculation.


Verification / Alternative check:
In a REPL: (endp nil) evaluates to t, and (endp (list 1)) evaluates to nil, confirming the predicate behavior.


Why Other Options Are Wrong:

Copying top-level list: performed by functions like copy-list, not endp.Returning length: performed by length, not endp.All of the above: mutually exclusive and incorrect.


Common Pitfalls:
Using null on non-list values without checks; forgetting endp’s role in proper list termination tests.


Final Answer:
It returns t if the list is empty

More Questions from Artificial Intelligence

Discussion & Comments

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