Common Lisp assignment: which expression correctly assigns the current value of a to b (i.e., after evaluation, b will equal a’s value)?

Difficulty: Easy

Correct Answer: (setq b a)

Explanation:


Introduction / Context:
Understanding assignment syntax is essential in Lisp, which uses prefix notation and special forms for variable binding and mutation. The task is to set variable b to the value currently held in a.


Given Data / Assumptions:

  • Dialect: Common Lisp.
  • Goal: after evaluation, b has the same value as a.
  • We are not creating a local binding; we are assigning.


Concept / Approach:
The special form setq performs assignment using the pattern (setq var value). Therefore, (setq b a) copies the value of a into b. Forms like (b = a) or (set b = a) reflect C-like or pseudo-code syntax and are invalid in Lisp. (setq a b) would reverse the assignment direction, setting a to b, not vice versa.


Step-by-Step Solution:

Identify the correct assignment operator in Common Lisp: setq.Apply the correct argument order: destination first (b), then source value (a).Select (setq b a).


Verification / Alternative check:
In a REPL: set a first (e.g., (setq a 42)), then evaluate (setq b a); now b evaluates to 42.


Why Other Options Are Wrong:

(setq a b): wrong direction.(b = a), (set b = a): not legal Common Lisp syntax.None of the above: incorrect because (setq b a) is correct.


Common Pitfalls:
Mixing up setq versus local binding with let, and reversing source/destination order.


Final Answer:
(setq b a)

More Questions from Artificial Intelligence

Discussion & Comments

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