如何在 Lisp 中创建数组列表,而不是它们的符号?

发布于 2024-08-14 01:28:54 字数 395 浏览 2 评论 0原文

我正在尝试创建一个函数来获取数组之间的增量,但现在只想创建一个子集:获取第 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

满身野味 2024-08-21 01:28:54

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.

指尖上的星空 2024-08-21 01:28:54

要获取包含两个向量之间差异的增量向量,请尝试以下操作:

(map 'vector #'- p2 p1)

在您的示例中,它返回:

#(2 2)

To get a delta vector containing the difference between two vectors, try this:

(map 'vector #'- p2 p1)

In your example, it returns:

#(2 2)
情域 2024-08-21 01:28:54

我认为问题在于您引用了该列表,即

'(p1 p2)

您应该引用

(list p1 p2)

该列表,因为在您的程序中您实际上尝试将 mapcar 应用于包含两个元素(符号 p1 和符号 p2)的列表。

I think the problem is that you have quoted the list, i.e.

'(p1 p2)

You should instead have

(list p1 p2)

because in your program you actually try to apply mapcar to a list containing two elements, the symbol p1 and the symbol p2.

﹂绝世的画 2024-08-21 01:28:54

antti.huima 说得对。但是,您的代码中存在另一个错误:

(mapcar #'(lambda (x) (aref x 0)) (list p1 p2))

请注意 lambda 之前的单引号之前的哈希标记。

antti.huima has it right. However, there is another error in your code:

(mapcar #'(lambda (x) (aref x 0)) (list p1 p2))

Note the hash mark before the single quote that precedes the lambda.

岁月蹉跎了容颜 2024-08-21 01:28:54

如果你愿意,你可以使用符号值:

(defvar p1 #(1 2))
(defvar p2 #(3 4))
(mapcar #'(lambda (x) (aref (symbol-value x) 0)) '(p1 p2))

If you want to, you can use SYMBOL-VALUE:

(defvar p1 #(1 2))
(defvar p2 #(3 4))
(mapcar #'(lambda (x) (aref (symbol-value x) 0)) '(p1 p2))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文