# LISP - 數組

LISP允許使用make-array函數來定義一個或多個維數組。一個數組可以任意LISP對象存儲爲它的元素。

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

aref 函數允許訪問該單元格的內容。它有兩個參數，數組名和索引值。

(aref my-array 9)

(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)

(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))

(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-yiibaier :displaced-to :displaced-index-offset

dimensions

:element-type

:initial-element

:initial-content

:fill-yiibaier

:displaced-to

:displaced-index-offset

(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))

(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))

;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 yiibaier 5, is 5 (write(length (make-array 14 :fill-yiibaier 5))) (terpri) ;however its length is 14 (write (array-dimensions (make-array 14 :fill-yiibaier 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))