Common Lisp predicates: which function returns t if and only if the supplied object is a number?
-
A(number <object>)
-
B(numberp <object>)
-
C(numericp <object>)
-
D(numeric <object>)
-
ENone of the above
Answer
Correct Answer: (numberp <object>)
Explanation
Introduction / Context: Lisp provides a rich set of predicates to test the type and properties of objects at runtime. Choosing the correct predicate prevents subtle bugs and clarifies intent in programs that manipulate heterogeneous data structures.
Given Data / Assumptions:
- We are using Common Lisp naming conventions for predicates.
- Return value t denotes true, nil denotes false.
- We want a test specifically for numbers (integers, rationals, floats, etc.).
Concept / Approach: In Common Lisp, predicates often end with the character p, indicating a question function that returns a boolean-like value. The standard numeric predicate is numberp. Variants like integerp and floatp exist for narrower categories. Names such as numericp or numeric are not standard predicates in Common Lisp, and (number would be parsed as a function call to a symbol number, not a built-in predicate.
Step-by-Step Solution:
Identify the naming convention: type predicates in Common Lisp use the p suffix.Recall that numberp is the general numeric predicate.Select(numberp as the correct choice.Verification / Alternative check: In a REPL, (numberp 3) and (numberp 3.14) both yield t, while (numberp 'abc) yields nil, confirming correctness.
Why Other Options Are Wrong:
(number (numericp None of the above: incorrect because numberp is correct.Common Pitfalls: Confusing Scheme or other dialect naming with Common Lisp conventions; forgetting more specific predicates like integerp when needed.
Final Answer: (numberp