I do not understand the following code :
(defun my-test ()
(let ((temp '(())))
(format t "temp: ~a~%" temp)
((lambda ()
(push 5 (first temp))))))
;; Execute this call twice in a row.
(my-test)
Output :
temp: (NIL)
temp: ((5))
How can temp
save the value ? I know there is the following warning, but I do not understand the logic behind this behavior.
; in: DEFUN MY-TEST
; (PUSH 5 (FIRST TEMP))
; --> LET*
; ==>
; (SB-KERNEL:%RPLACA #:TEMP0 (CONS 5 (FIRST #:TEMP0)))
;
; caught WARNING:
; Destructive function SB-KERNEL:%RPLACA called on constant data: (NIL)
; See also:
; The ANSI Standard, Special Operator QUOTE
; The ANSI Standard, Section 3.2.2.3
;
; compilation unit finished
; caught 1 WARNING condition
The following codes output the same result :
(flet ((my-fct ()
(let ((temp '(())))
(format t "temp: ~a~%" temp)
((lambda ()
(push 5 (first temp)))))))
(my-fct)
(my-fct))
(let ((fct (lambda ()
(let ((temp '(())))
(format t "temp: ~a~%" temp)
((lambda ()
(push 5 (first temp))))))))
(funcall fct)
(funcall fct))
But this one works :
;; Execute this call twice in a row.
((lambda ()
(let ((temp '(())))
(format t "temp: ~a~%" temp)
((lambda ()
(push 5 (first temp)))))))
This one works too :
(defun my-test ()
(let ((temp (list ())))
(format t "temp: ~a~%" temp)
((lambda ()
(push 5 (first temp))))))
(my-test)
And this one too :
(defun my-test ()
(let ((temp (list (list))))
(format t "temp: ~a~%" temp)
((lambda ()
(push 5 (first temp))))))
(my-test)
But not this one :
(defun my-test ()
(let ((temp `(,(list))))
(format t "temp: ~a~%" temp)
((lambda ()
(push 5 (first temp))))))
(my-test)
- Why does it work for some codes, and not for some others ?
- How can the lexical value can be conserved across multiple calls ?