我对这段代码的工作方式感到困惑:
(define m (list 1 2 3 '(5 8)))
(let ((l (cdr m)))
(set! l '(28 88))) ==>(1 2 3 (5 8))
(define o (list 1 2 3 '(5 8)))
(let ((l (cdr o)))
(set-car! l '(28 88))) ==> (1 (28 88) 3 (5 8))
为什么 (set!l '(28 88)))
不更新 m
?
最佳答案
设置!不能改变列表(或任何其他结构,不像 set-car/cdr!),但只能改变一个(在你的情况下,本地 l)变量的绑定(bind)。
(define x 3)
(define f (lambda (x) (set! x 4)))
(define g (lambda (y) (set! x y)))
(f x)
x
-> 3
(g 5)
x
-> 5
关于方案:与集合混淆!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2985093/