setf

8 views
Skip to first unread message

panfei

unread,
Oct 12, 2012, 11:05:27 PM10/12/12
to lisp-...@googlegroups.com
[28]> (setf *glob* 98)
98
[29]> (let ((n 10))
(setf n 2) n)
2
[30]> (+ 1 n)

*** - SYSTEM::READ-EVAL-PRINT: variable N has no value
The following restarts are available:
USE-VALUE      :R1      Input a value to be used instead of N.
STORE-VALUE    :R2      Input a new value for N.
ABORT          :R3      Abort main loop
Break 1 [31]> :R3
[32]> 

总结: 当setf的第一个参数不是本地变量符号的时候,它就取全局变量。

[32]> (setf x (list 'a 'b 'c))
(A B C)
[33]> (x)

*** - EVAL: undefined function X
The following restarts are available:
USE-VALUE      :R1      Input a value to be used instead of (FDEFINITION 'X).
RETRY          :R2      Retry
STORE-VALUE    :R3      Input a new value for (FDEFINITION 'X).
ABORT          :R4      Abort main loop
Break 1 [34]> :R3
New (FDEFINITION 'X)> R:3

*** - READ from #<IO SYNONYM-STREAM *TERMINAL-IO*>: there is no package with name "R"
The following restarts are available:
ABORT          :R1      Abort debug loop
ABORT          :R2      Abort main loop
Break 2 [35]> :R2
[36]> 'x
X
[37]> (con
concatenate                  cond                         conjugate                    consp                        constantp                    convert-attributes           convert-string-from-bytes    
concatenated-stream-streams  confstr                      cons                         constantly                   continue                     convert-mode                 convert-string-to-bytes      
[37]> (con
concatenate                  cond                         conjugate                    consp                        constantp                    convert-attributes           convert-string-from-bytes    
concatenated-stream-streams  confstr                      cons                         constantly                   continue                     convert-mode                 convert-string-to-bytes      
[37]> (cond x)

*** - COND: clause X should be a list
The following restarts are available:
ABORT          :R1      Abort main loop
Break 1 [38]> :R2
:R2
Break 1 [38]> :R1
[39]> (cond 'x)

*** - COND: variable QUOTE has no value
The following restarts are available:
USE-VALUE      :R1      Input a value to be used instead of QUOTE.
STORE-VALUE    :R2      Input a new value for QUOTE.
ABORT          :R3      Abort main loop
Break 1 [40]> :R3
[41]> (car x)
A
[42]> (setf (car x) 'n)
N
[43]> x
(N B C)
[44]> 

总结:你可以仅仅通过赋值来隐式地创建全局变量,在源文件里使用显式的defparameters是更好的风格。你不仅仅可以赋值给一个变量。setf的第一个参数可以是一个表达式。这种情况下,第二个参数的值将会被插入到第一个参数所引用的位置。

--
不学习,不知道

Reply all
Reply to author
Forward
0 new messages