我可能在R5RS文件中错过了这个但是如何在(鸡)计划中创建列表?我希望能够列出一个列表a
,调用(list-ref a b)
,将结果分配给c
,然后调用(list-ref c d)
,其中b
和{ {1}}是索引值。
编辑:为了澄清,假设我有这些列表:
d
然后我想创建一个名为(define citrus (list "oranges" "limes"))
(define apples (list "macintosh" "rome" "delicious"))
的列表,其中fruit
和citrus
为列表条目。
答案 0 :(得分:3)
以下是创建列表列表的方法:
(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,它位于apples子列表中的索引0
处:)
(list-ref (list-ref fruit 1) 0)
=> "macintosh"
答案 1 :(得分:2)
如果你想制作一个包含这些列表的列表,只需用它们作为参数调用list
:
(define fruit (list citrus apples))
(list-ref (list-ref fruit 0) 1)
=> "lime"