Common Lisp literal symbols: Which form correctly assigns the symbol x (not the value of x) to the variable y?

Computer Science Artificial Intelligence Difficulty: Easy
Choose an option
  • A
    (setq y x)
  • B
    (set y = 'x')
  • C
    (setq y = 'x')
  • D
    (setq y 'x')
  • E
    None of the above

Answer

Correct Answer: (setq y 'x')

Explanation

Introduction / Context:In Lisp, symbols can be manipulated as data. To store the literal symbol x in a variable y, you must prevent evaluation of x. The quote operator (' or (quote …)) serves this purpose by yielding the symbol itself instead of its bound value.

Given Data / Assumptions:

  • We want y to hold the symbol x, not x's current value.
  • setq assigns to a variable without evaluating the variable name.
  • We must ensure the right-hand side is the symbol object.

Concept / Approach:Use (setq y 'x'). Here, 'x evaluates to the symbol X. setq stores that symbol in y. Any forms that use ‘‘=’’ syntax are not Lisp and are therefore incorrect.

Step-by-Step Solution:

Prevent evaluation of x using quote: 'x.Assign it with setq: (setq y 'x').Confirm: evaluating y should return the symbol X.If needed, compare to (setq y x), which would copy x's current value instead.

Verification / Alternative check:In a REPL, after (setq y 'x'), (symbolp y) returns T and (eq y 'x') returns T.

Why Other Options Are Wrong:

  • (setq y x) assigns the value of x, not the symbol.
  • Forms using ‘‘=’’ are not valid Lisp syntax.

Common Pitfalls:Forgetting to quote the symbol or confusing set and setq semantics can result in unintended variable dereferencing.

Final Answer:(setq y 'x')

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