首先lexical-binding没问题
(lambda (seq) (cdr seq))
;;=> (closure (t) (seq) (cdr seq))
我以为会返回1,因为我把cdr的函数设为了car:
(funcall (cl-letf (((symbol-function 'cdr) (symbol-function 'car))
((symbol-function 'car) (symbol-function 'cdr)))
(lambda (seq)
(cdr seq)))
'(1 . 2))
;;=> 2
直接用没问题:
(cl-letf (((symbol-function 'cdr) (symbol-function 'car))
((symbol-function 'car) (symbol-function 'cdr)))
(cdr '(1 . 2)))
;;=> 1
closure是这样的:
(cl-letf (((symbol-function 'cdr) (symbol-function 'car))
((symbol-function 'car) (symbol-function 'cdr)))
(lambda (seq)
(cdr seq)))
(closure ((old . #<subr cdr>) (old . #<subr car>) (vnew . #<subr cdr>) (vnew . #<subr car>) t) (seq) (cdr seq))
P.S. 据文档说,((old . #<subr cdr>) (old . #<subr car>) (vnew . #<subr cdr>) (vnew . #<subr car>) t)
是lexical environment
,但我看不出来是怎么用的。
是我对closure有什么误解么?