LISP - 數組

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

所有數組組成的連續的存儲單元。最低的地址對應於第一個元素和最高地址的最後一個元素。

rank

數組的維數被稱為它的秩。

在LISP語言中,數組元素是由一個非負整數索引的順序指定。該序列的長度必須等於數組的秩。索引從0開始。

例如,要創建一個數組,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-zaixianer  :displaced-to :displaced-index-offset

除了維度參數,所有其他參數都是關鍵字。下表提供的參數簡要說明。

參數 描述
dimensions 它給該數組的大小。它是一個數字為一維數組,而對於多維數組列表。
:element-type 它是類型說明符,默認值是T,即任何類型
:initial-element 初始元素值。它將使一個數組的所有初始化為一個特定值的元素。
:initial-content 初始內容作為對象。
:adjustable 它有助於創造一個可調整大小(或可調)向量,其底層的記憶體可以調整大小。該參數是一個布爾值,表示數組是否可調與否,默認值是nil。
:fill-zaixianer 它跟蹤實際存儲在一個可調整大小的向量元素的數目
:displaced-to 它有助於創造一個移位的數組或共用數組共用其內容與指定的數組。這兩個數組應該有相同的元素類型。位移到選項可能無法使用:displaced-to或:initial-contents選項。此參數默認為nil。
:displaced-index-offset 它給出了索引偏移創建的共用數組。

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

上一篇: LISP - 字元 下一篇: LISP - 符號