如何在 Lisp 中创建数组列表,而不是它们的符号?
我正在尝试创建一个函数来获取数组之间的增量,但现在只想创建一个子集:获取第 N 个元素。
(defvar p1 #(1 2))
(defvar p2 #(3 4))
(mapcar '(lambda (x) (aref x 0)) '(p1 p2))
debugger invoked on a TYPE-ERROR in ...
The value P1 is not of type ARRAY.
如果我用 make-array 来实现同样的错误。
在一般情况下如何应用 lambda 函数,或者如何应用 (aref x 0)
或 (aref x N)
?
最后我想创建一个返回增量的函数:p2 - p1。
I'm trying to make a function to get a delta between arrays, but right now just want to make a subset: get Nth element.
(defvar p1 #(1 2))
(defvar p2 #(3 4))
(mapcar '(lambda (x) (aref x 0)) '(p1 p2))
debugger invoked on a TYPE-ERROR in ...
The value P1 is not of type ARRAY.
The same error if I make it with make-array.
How do I apply the lambda function, or how to apply (aref x 0)
, or (aref x N)
in general case?
In the end I want to make a function that returns a delta: p2 - p1.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
MAPCAR 将函数作为第一个参数。
'(lambda (x) (aref x 0))
与(quote (lambda (x) (aref x 0)))
相同,这是 不是函数。您希望使用(function (lambda (x) (aref x 0)))
使其成为一个函数,可以将其写得更短为#'(lambda (x) (aref x 0 ))
,甚至(因为标准宏)(lambda (x) (aref x 0))
。'(p1 p2)
与(quote (p1 p2))
相同。 QUOTE 表示参数未计算,因此名称“P1”和“P2”代表它们本身,而不代表它们的值。您得到的类型错误是符号'P1
不是数组,它只是有一个数组作为值。要获取值列表,请使用 LIST:(list p1 p2)
。结论:
(mapcar #'(lambda (x) (aref x 0)) (list p1 p2))
编辑:要减去向量,您应该查看 MAP 函数;请注意,您可以提供多个序列。
MAPCAR takes a function as first argument.
'(lambda (x) (aref x 0))
is the same as(quote (lambda (x) (aref x 0)))
, and this is not a function. You want to make it a function with(function (lambda (x) (aref x 0)))
, which can be written shorter as#'(lambda (x) (aref x 0))
, or even (because of a standard macro)(lambda (x) (aref x 0))
.'(p1 p2)
is the same as(quote (p1 p2))
. QUOTE means that the arguments are not evaluated, so the names "P1" and "P2" stand for themselves, not for their values. The type error you get is that the symbol'P1
is not an array, it just has an array as value. In order to get a list of the values, use LIST:(list p1 p2)
.In conclusion:
(mapcar #'(lambda (x) (aref x 0)) (list p1 p2))
EDIT: For subtracting vectors, you should look into the MAP function; note that you can provide multiple sequences.
要获取包含两个向量之间差异的增量向量,请尝试以下操作:
在您的示例中,它返回:
To get a delta vector containing the difference between two vectors, try this:
In your example, it returns:
我认为问题在于您引用了该列表,即
您应该引用
该列表,因为在您的程序中您实际上尝试将 mapcar 应用于包含两个元素(符号 p1 和符号 p2)的列表。
I think the problem is that you have quoted the list, i.e.
You should instead have
because in your program you actually try to apply mapcar to a list containing two elements, the symbol p1 and the symbol p2.
antti.huima 说得对。但是,您的代码中存在另一个错误:
请注意
lambda
之前的单引号之前的哈希标记。antti.huima has it right. However, there is another error in your code:
Note the hash mark before the single quote that precedes the
lambda
.如果你愿意,你可以使用符号值:
If you want to, you can use SYMBOL-VALUE: