Common Lisp assignment semantics: Which function evaluates both the variable argument and the value expression when assigning, thereby allowing indirect (symbol-valued) assignment?

Difficulty: Easy

Correct Answer: set

Explanation:


Introduction / Context:
In Common Lisp, variable assignment can be done with multiple forms. Understanding when the variable name is evaluated versus treated as a literal symbol is crucial, especially for indirect assignments and metaprogramming tasks.



Given Data / Assumptions:

  • We compare set and setq semantics.
  • We care about whether the first argument (the variable) is evaluated.
  • Goal: identify the form that evaluates both its arguments.


Concept / Approach:
setq assigns to a named variable without evaluating the variable name; only the value expression is evaluated. By contrast, set evaluates its first argument to obtain a symbol and evaluates its second argument for the new value. Thus, set supports indirect assignment when the symbol to assign is itself computed.



Step-by-Step Solution:

Consider (setq x 10): ‘‘x’’ is not evaluated; value 10 is stored in variable x.Consider (set (read-from-string "x") 10): first argument evaluates to the symbol X; second evaluates to 10; X is then assigned 10.Therefore, the function that evaluates both variable and value is set.Select ‘‘set’’ as the correct choice.


Verification / Alternative check:
Test in a REPL: (let ((s 'x')) (set (symbol-value 's') 5)) demonstrates evaluated first arg; (setq s 5) would instead set variable s, not X.



Why Other Options Are Wrong:

  • setq does not evaluate the variable name.
  • add and ‘‘eva’’ are not standard Common Lisp assignment primitives.


Common Pitfalls:
Passing a raw symbol to set without quoting or ensuring evaluation yields a symbol leads to errors; ensure the first argument evaluates to a symbol object.



Final Answer:
set

More Questions from Artificial Intelligence

Discussion & Comments

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