In LISP, which predicate returns t (true) if the supplied is a CONS cell and returns nil (false) otherwise?
Computer Science Artificial Intelligence Difficulty: Easy
Choose an option
  • A
    (consp <object>)
  • B
    (eq <object>)
  • C
    (cons <object>)
  • D
    (cous = <object>)
  • E
    None of the above

Answer

Correct Answer: (consp <object>)

Explanation

Introduction / Context:LISP lists are built from CONS cells—pairs consisting of a head (car) and a tail (cdr). Recognizing list structure at runtime is a common need in symbolic processing. LISP provides type predicates to test the nature of objects. This question asks for the predicate that checks whether an object is specifically a CONS cell.

Given Data / Assumptions:

  • We need a boolean test that distinguishes CONS cells from atoms or other objects.
  • The result should be t for CONS and nil otherwise.
  • Standard Common Lisp predicate names are assumed.

Concept / Approach:

The predicate consp returns t when its argument is a CONS cell. By contrast, cons constructs a new CONS cell and is not a predicate, and eq tests identity between two objects, not type. The misspelled form ‘‘(cous = )’’ is invalid syntax and not a standard function.

Step-by-Step Solution:

Recall standard predicates: consp, listp, atom, null, etc.Match the requirement ‘‘true if argument is a CONS’’ to consp.Eliminate constructor and equality functions (cons, eq) and invalid tokens.Select ‘‘(consp )’’ as correct.

Verification / Alternative check:

In a Common Lisp REPL: (consp '(a b)) → t; (consp 'a) → nil. This confirms the predicate's behavior.

Why Other Options Are Wrong:

(cons ): Creates a cell; does not test type.

(eq ): Compares two arguments for identity; wrong arity and purpose here.

(cous = ): Not valid LISP syntax or function.

None: Incorrect because consp is the precise predicate.

Common Pitfalls:

Confusing constructors with predicates; overlooking arity requirements in predicate forms.

Final Answer:

(consp )

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