Common Lisp predicate basics: which function returns t when and only when the provided object is a symbol?
-
A(* <object>)
-
B(symbolp <object>)
-
C(nonnumeric <object>)
-
D(constantp <object>)
-
ENone of the above
Answer
Correct Answer: (symbolp <object>)
Explanation
Introduction / Context: Lisp code frequently inspects data types at runtime. Recognizing the correct predicate for symbols is fundamental, especially when manipulating code-as-data, macros, or abstract syntax trees where symbols serve as identifiers.
Given Data / Assumptions:
- The dialect is Common Lisp.
- Predicates often end with p and return t or nil.
- We need a general test for “is this value a symbol?”
Concept / Approach: The standard predicate for symbols is symbolp. Alternatives listed either are not predicates of the right kind or test different properties: constantp checks whether a form is a compile-time constant, while (* is an arithmetic call, not a predicate. There is no standard nonnumeric predicate in Common Lisp.
Step-by-Step Solution:
Identify candidate predicates that match Common Lisp naming conventions.Recall thatsymbolp returns t for symbols and nil otherwise.Select (symbolp .Verification / Alternative check: In a REPL: (symbolp 'x) → t; (symbolp 42) → nil. This confirms the behavior.
Why Other Options Are Wrong:
(* (nonnumeric (constantp None of the above: incorrect becausesymbolp is correct.Common Pitfalls: Confusing symbol tests with string or keyword tests; remember keywords are symbols too.
Final Answer: (symbolp