📜  LISP-阵列

📅  最后修改于: 2020-11-03 07:12:26             🧑  作者: Mango


LISP允许您使用make-array函数定义一维或多维数组。数组可以存储任何LISP对象作为其元素。

所有阵列均包含连续的内存位置。最低地址对应于第一个元素,最高地址对应于最后一个元素。

秩

数组的维数称为其秩。

在LISP中,数组元素由一系列非负整数索引指定。序列的长度必须等于数组的秩。索引从零开始。

例如,要创建一个包含10个单元的数组my-array,我们可以编写-

(setf my-array (make-array '(10)))

aref函数允许访问单元格的内容。它有两个参数,数组的名称和索引值。

例如,要访问第十个单元格的内容,我们编写-

(aref my-array 9)

例子1

创建一个名为main.lisp的新源代码文件,然后在其中键入以下代码。

(write (setf my-array (make-array '(10))))
(terpri)
(setf (aref my-array 0) 25)
(setf (aref my-array 1) 23)
(setf (aref my-array 2) 45)
(setf (aref my-array 3) 10)
(setf (aref my-array 4) 20)
(setf (aref my-array 5) 17)
(setf (aref my-array 6) 25)
(setf (aref my-array 7) 19)
(setf (aref my-array 8) 67)
(setf (aref my-array 9) 30)
(write my-array)

当您执行代码时,它返回以下结果-

#(NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL)
#(25 23 45 10 20 17 25 19 67 30)

例子2

让我们创建一个3×3数组。

创建一个名为main.lisp的新源代码文件,然后在其中键入以下代码。

(setf x (make-array '(3 3) 
   :initial-contents '((0 1 2 ) (3 4 5) (6 7 8)))
)
(write x)

当您执行代码时,它返回以下结果-

#2A((0 1 2) (3 4 5) (6 7 8))

例子3

创建一个名为main.lisp的新源代码文件,然后在其中键入以下代码。

(setq a (make-array '(4 3)))
(dotimes (i 4)
   (dotimes (j 3)
      (setf (aref a i j) (list i 'x j '= (* i j)))
   )
)
(dotimes (i 4)
   (dotimes (j 3)
      (print (aref a i j))
   )
)

当您执行代码时,它返回以下结果-

(0 X 0 = 0) 
(0 X 1 = 0) 
(0 X 2 = 0) 
(1 X 0 = 0) 
(1 X 1 = 1) 
(1 X 2 = 2) 
(2 X 0 = 0) 
(2 X 1 = 2) 
(2 X 2 = 4) 
(3 X 0 = 0) 
(3 X 1 = 3) 
(3 X 2 = 6)

make-array函数的完整语法

make-array函数采用许多其他参数。让我们看看这个函数的完整语法-

make-array dimensions :element-type :initial-element :initial-contents :adjustable :fill-pointer  :displaced-to :displaced-index-offset

除了Dimensions参数之外,所有其他参数都是关键字。下表提供了参数的简要说明。

Sr.No. Argument & Description
1

dimensions

It gives the dimensions of the array. It is a number for one-dimensional array, and a list for multi-dimensional array.

2

:element-type

It is the type specifier, default value is T, i.e. any type

3

:initial-element

Initial elements value. It will make an array with all the elements initialized to a particular value.

4

:initial-content

Initial content as object.

5

:adjustable

It helps in creating a resizeable (or adjustable) vector whose underlying memory can be resized. The argument is a Boolean value indicating whether the array is adjustable or not, default value being NIL.

6

:fill-pointer

It keeps track of the number of elements actually stored in a resizeable vector.

7

:displaced-to

It helps in creating a displaced array or shared array that shares its contents with the specified array. Both the arrays should have same element type. The :displaced-to option may not be used with the :initial-element or :initial-contents option. This argument defaults to nil.

8

:displaced-index-offset

It gives the index-offset of the created shared array.

例子4

创建一个名为main.lisp的新源代码文件,然后在其中键入以下代码。

(setq myarray (make-array '(3 2 3) 
   :initial-contents 
   '(((a b c) (1 2 3)) 
      ((d e f) (4 5 6)) 
      ((g h i) (7 8 9)) 
   ))
) 
(setq array2 (make-array 4 :displaced-to myarray :displaced-index-offset 2)) 
(write myarray)
(terpri)
(write array2)

当您执行代码时,它返回以下结果-

#3A(((A B C) (1 2 3)) ((D E F) (4 5 6)) ((G H I) (7 8 9)))
#(C 1 2 3)

如果位移数组是二维的-

(setq myarray (make-array '(3 2 3) 
   :initial-contents 
   '(((a b c) (1 2 3)) 
      ((d e f) (4 5 6)) 
      ((g h i) (7 8 9)) 
   ))
) 
(setq array2 (make-array '(3 2) :displaced-to myarray :displaced-index-offset 2)) 
(write myarray)
(terpri)
(write array2)

当您执行代码时,它返回以下结果-

#3A(((A B C) (1 2 3)) ((D E F) (4 5 6)) ((G H I) (7 8 9)))
#2A((C 1) (2 3) (D E))

让我们将置换索引偏移量更改为5-

(setq myarray (make-array '(3 2 3) 
   :initial-contents 
   '(((a b c) (1 2 3)) 
      ((d e f) (4 5 6)) 
      ((g h i) (7 8 9)) 
   ))
) 
(setq array2 (make-array '(3 2) :displaced-to myarray :displaced-index-offset 5)) 
(write myarray)
(terpri)
(write array2)

当您执行代码时,它返回以下结果-

#3A(((A B C) (1 2 3)) ((D E F) (4 5 6)) ((G H I) (7 8 9)))
#2A((3 D) (E F) (4 5))

例子5

创建一个名为main.lisp的新源代码文件,然后在其中键入以下代码。

;a one dimensional array with 5 elements, 
;initail value 5
(write (make-array 5 :initial-element 5))
(terpri)

;two dimensional array, with initial element a
(write (make-array '(2 3) :initial-element 'a))
(terpri)

;an array of capacity 14, but fill pointer 5, is 5
(write(length (make-array 14 :fill-pointer 5)))
(terpri)

;however its length is 14
(write (array-dimensions (make-array 14 :fill-pointer 5)))
(terpri)

; a bit array with all initial elements set to 1
(write(make-array 10 :element-type 'bit :initial-element 1))
(terpri)

; a character array with all initial elements set to a
; is a string actually
(write(make-array 10 :element-type 'character :initial-element #\a)) 
(terpri)

; a two dimensional array with initial values a
(setq myarray (make-array '(2 2) :initial-element 'a :adjustable t))
(write myarray)
(terpri)

;readjusting the array
(adjust-array myarray '(1 3) :initial-element 'b) 
(write myarray)

当您执行代码时,它返回以下结果-

#(5 5 5 5 5)
#2A((A A A) (A A A))
5
(14)
#*1111111111
"aaaaaaaaaa"
#2A((A A) (A A))
#2A((A A B))