如何在elisp中设置多维数组中的元素 [英] How to set an element in a multi-dimensional array in elisp

查看:98
本文介绍了如何在elisp中设置多维数组中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在和使用elisp的人一起工作,我们一直在努力使用多维数组.

I am working with someone using elisp and we have been struggling to use multi-dimensional arrays.

问题在于,如果我们尝试使用设置值

The problem is that if we try to set a value using

(setf (elt (elt m-array 0) 0) 5))

我们最终得到这样的东西

We end up getting something like this

[[0 0 0 5] [0 0 0 5] [0 0 0 5] [0 0 0 5]]

这不是我们想要的.现在,Common Lisp已获得我们需要解决的支持.不幸的是,我们只能使用elisp.我的问题是,鉴于我们只有elisp,我们如何解决这个问题,而只能在向量中设置一个向量.

which is not what we want. Now Common Lisp has the support we need to get around this. Unfortunately though, we are only able to work with elisp. My question is, given that we only have elisp, how can we work around this such that we instead only set one vector in the vectors.

赞:

[[0 0 0 5] [0 0 0 0] [0 0 0 0] [0 0 0 0]]

推荐答案

Common Lisp具有多维数组,Emacs Lisp只有 vectors (一维数组).

While Common Lisp has multidimensional arrays, Emacs Lisp has only vectors (one-dimensional arrays).

您正在尝试使用向量向量在ELisp中模拟多维数组(这确实是一个相当标准的技巧),但是您需要小心避免混淆"-即,您需要确保嵌套数组不是相同的对象.

You are trying to emulate multidimensional arrays in ELisp using vectors of vectors (which is, indeed, a fairly standard trick), but you need to be careful to avoid "aliasing" - i.e., you need to make sure that your nested arrays are not identical objects.

您的问题表明是

(eq (aref m-array 0) (aref m-array 1))
==> t

因为您可能这样创建了m-array:

because you probably created your m-array like this:

(setq m-array (make-vector 5 (make-vector 5)))

您需要像这样创建m-array:

(setq m-array (make-vector 5 nil))
(dotimes (i 5)
  (setf (aref m-array i) (make-vector 5 0)))

这篇关于如何在elisp中设置多维数组中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆