Programming

(Common Lisp) set, setq 비교

steloflute 2023. 10. 26. 23:31

SBCL에서,

 

* (set 'a 3)
3
* a
3
* (describe 'a)
COMMON-LISP-USER::A
  [symbol]

A names an undefined variable:
  Value: 3
* (setq b 4)
; in: SETQ B
;     (SETQ B 4)
;
; caught WARNING:
;   undefined variable: COMMON-LISP-USER::B
;
; compilation unit finished
;   Undefined variable:
;     B
;   caught 1 WARNING condition
4
* (describe 'b)
COMMON-LISP-USER::B
  [symbol]

B names an undefined variable:
  Value: 4

(set 'a)는 warning이 안 난다.

이유는:

Difference between `set`, `setq`, and `setf` in Common Lisp? - Stack Overflow

와 같이 setq는 (setf (symbol-value 'c) 4) 의 의미이기 때문이다.

 

CLHS: Accessor SYMBOL-VALUE (lisp.se)