问题描述
这就是问题:我不会获取 setf扩展器,而是想学习它们的工作原理。
Here's the thing: I don't "get" setf-expanders and would like to learn how they work.
我需要学习它们的工作原理,因为我有一个问题似乎是为什么您应该学习setf-expanders的典型示例,问题如下:
I need to learn how they work because I've got a problem which seems like a typical example for why you should learn setf-expanders, the problem is as follows:
(defparameter some-array (make-array 10))
(defun arr-index (index-string)
(aref some-array (parse-integer index-string))
(setf (arr-index "2") 7) ;; Error: undefined function (setf arr-index)
如何为ARR-INDEX编写适当的setf-expander?
How do I write a proper setf-expander for ARR-INDEX?
推荐答案
(defun (setf arr-index) (new-value index-string)
(setf (aref some-array (parse-integer index-string))
new-value))
在Common Lisp中不仅可以是符号,还可以是符号以 SETF
作为第一个符号的两个符号的列表。往上看。 DEFUN
因此可以定义 SETF
函数。函数的名称为(setf arr-index)
。
In Common Lisp a function name can not only be a symbol, but also a list of two symbols with SETF
as the first symbol. See above. DEFUN
thus can define SETF
functions. The name of the function is (setf arr-index)
.
一个 setf函数可以以 place 形式使用:。
A setf function can be used in a place form: CLHS: Other compound forms as places.
新值是第一个参数。
CL-USER 15 > some-array
#(NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL)
CL-USER 16 > (setf (arr-index "2") 7)
7
CL-USER 17 > some-array
#(NIL NIL 7 NIL NIL NIL NIL NIL NIL NIL)
这篇关于在Common Lisp中定义setf-expanders的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!