方案:与集合混淆!

sup*_*uay 2 scheme racket

我很困惑这段代码是如何工作的:

(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))
Run Code Online (Sandbox Code Playgroud)

为什么不(set! l '(28 88)))更新m

Rha*_*aun 7

组!不能改变列表(或任何其他结构,不像set-car/cdr!),但只改变a(在你的情况下,本地l)变量的绑定.

(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
Run Code Online (Sandbox Code Playgroud)