Common lisp lisp中的动态二维数组
我想在Lisp中有一个2D数组。但每行可以有不同数量的元素(最多5个)。因此,我考虑维护另一个列表来存储每行的当前大小,并在需要时进行更新 我的代码是这样的:Common lisp lisp中的动态二维数组,common-lisp,clisp,Common Lisp,Clisp,我想在Lisp中有一个2D数组。但每行可以有不同数量的元素(最多5个)。因此,我考虑维护另一个列表来存储每行的当前大小,并在需要时进行更新 我的代码是这样的: (setq N (read)) (setq myMatrix (make-array (list N 5))) (setq sizeArray (make-array N:fill-pointer N)) (dotimes (i N) (setf (aref sizeArray i) 0) ) (dotimes (i N)
(setq N (read))
(setq myMatrix (make-array (list N 5)))
(setq sizeArray (make-array N:fill-pointer N))
(dotimes (i N)
(setf (aref sizeArray i) 0)
)
(dotimes (i N)
//Here I need to take input in each row until user presses -1.It is sure he can't insert more than 5 items in each row.
)
(setq item (read-line))
(setf (aref myMatrix i (nthcdr i sizeArray)) item)
现在,我有一个循环来填充每行的元素,如下所示:
(setq N (read))
(setq myMatrix (make-array (list N 5)))
(setq sizeArray (make-array N:fill-pointer N))
(dotimes (i N)
(setf (aref sizeArray i) 0)
)
(dotimes (i N)
//Here I need to take input in each row until user presses -1.It is sure he can't insert more than 5 items in each row.
)
(setq item (read-line))
(setf (aref myMatrix i (nthcdr i sizeArray)) item)
如何做到这一点?请帮忙。我试过这样的方法:
(setq N (read))
(setq myMatrix (make-array (list N 5)))
(setq sizeArray (make-array N:fill-pointer N))
(dotimes (i N)
(setf (aref sizeArray i) 0)
)
(dotimes (i N)
//Here I need to take input in each row until user presses -1.It is sure he can't insert more than 5 items in each row.
)
(setq item (read-line))
(setf (aref myMatrix i (nthcdr i sizeArray)) item)
但是它不起作用。公共Lisp中的数组可以有填充指针,这提供了一种将数组视为可以增长和扩展其长度的对象的方法。二维数组可以实现为带有填充指针的数组数组。例如:
(defun make-row-resizeable-array (rows max-columns)
"Returns an array of length ROWS containing arrays
of length MAX-COLUMNS, but with a fill pointer initially
set to 0."
(make-array rows
:initial-contents (loop for i from 0 below rows
collect (make-array max-columns
:fill-pointer 0))))
这将使您能够:
CL-USER> (let ((array (make-row-resizeable-array 6 5)))
(vector-push 'x (aref array 2))
(vector-push 'y (aref array 2))
(vector-push 'z (aref array 2))
(vector-push 'a (aref array 3))
(vector-push 'b (aref array 3))
array)
;=> #(#() #() #(X Y Z) #(A B) #() #())
您也可以轻松获得长度:
CL-USER> (map 'list 'length *)
;=> (0 0 3 2 0 0)
您可以通过嵌套的aref调用从数组中获取单个元素:
(let ((array (make-row-resizeable-array 6 5)))
(vector-push 'x (aref array 2))
(vector-push 'y (aref array 2))
(vector-push 'z (aref array 2))
(vector-push 'a (aref array 3))
(vector-push 'b (aref array 3))
(let ((row 3)
(col 1))
(aref (aref array row) col)))
;=> B
哪种方式不起作用?无论如何,与使用2D数组和跟踪每行中的元素数量不同,我认为使用一些虚拟值(例如
-1
或nil
)填充未使用的位置或使用向量向量向量更容易。Nthcdr
在列表上工作,而不是在数组上。我想您需要(aref size array I)
在那里。N:fill pointer
是包N
中的符号fill pointer
。我认为您缺少了一个空格。请注意,默认的readtable将其读取的所有内容都加上了大小写,因此camel大小写没有任何意义。而且,/
没有引入注释。行注释以一个或多个开头代码>。如何获取第I行第j列的值,比如我想要第3行第一列的值,比如:XMain问题不是填充数组,而是访问元素。这就像访问数组一样,你只需要说(aref(aref…)
)。我刚刚做了一个更新。“主要问题不是填充数组,而是访问元素”,而我看到您在问题的最后一句中询问了这一点,标题和第一句话“lisp中的动态2d数组;我希望lisp中有一个2d数组”听起来像是您希望有一种方法,使2d数组具有动态大小的行。至于设置它们,我用矢量推的方法说明了这一点。请注意,如果执行(dotime(i行)(用户输入循环(向量推送(用户输入)(aref数组i))
,则可以构建所需的结构。那么只访问第i行的长度,而不是整个矩阵的长度呢