這裏是你如何創建一個列表的列表:
(list (list 1 2) (list 3 4))
或者更簡單:
'((1 2) (3 4))
現在,如果你已經定義爲單獨列出其他子列表,把它們的外內名單上再次呼籲list
:
(define the-first (list 1 2))
(define the-second (list 3 4))
(define list-of-lists (list the-first the-second))
list-of-lists
=> '((1 2) (3 4))
要訪問給定兩個職位前男友,這樣做 - 記住,索引是從零開始:
(define lst '((1 2) (3 4)))
(list-ref (list-ref lst 1) 0)
=> 3
所以,在這個問題的第一個例子是這樣的:
(define a '((1 2) (3 4)))
(define b 1)
(define c (list-ref a b))
(define d 0)
(list-ref c d)
=> 3
第二個示例(編輯後)將是這樣的:
(define citrus (list "oranges" "limes"))
(define apples (list "macintosh" "rome" "delicious"))
(define fruit (list citrus apples)) ; here's the list of lists
現在,首先訪問一個元素,我們必須通過最外層列表的指數(比方說,我們希望蘋果,這是在最外層列表索引1
)和日恩最裏面的列表指數(讓我們說,我們要的是Macintosh,它是在指數0
在蘋果子表):
(list-ref (list-ref fruit 1) 0)
=> "macintosh"
代碼錯誤,給定的索引返回''limes「',而不是'」羅馬「' –