简体   繁体   English

球拍lang:列表中的访问元素

[英]Racket lang: access element in list

Give a single function to access the element a in the list L. 提供单个功能以访问列表L中的元素a。

(define L '(1 2 (a 3 4 5)))

Following the form (define id expr) which binds id to the result of expression I have tried the following: 按照将id绑定到表达式结果的形式(定义id expr),我尝试了以下操作:

(define L '(1 2 (a 3 4 5) (car(cdr L))))

cdr accesses the tail of the list, ie a 3 4 5, if I am not mistaken, and then I apply car on the tail to access the head of the list, ie a. cdr访问列表的末尾,即3 4 5,如果我没记错的话,然后我将car放在列表的末尾以访问列表的头,即a。 However, it is not working on DrRacket IDE. 但是,它在DrRacket IDE上不起作用。

I think you meant to do this: 我认为您打算这样做:

(define L '(1 2 (a 3 4 5)))

(car (car (cdr (cdr L))))
=> 'a

Which can also be written as: 也可以写成:

(caaddr L)
=> 'a

You included the (car(cdr L)) part inside the list L . 您将(car(cdr L))部分包括在列表L

> (define L '(1 2 (a 3 4 5) (car(cdr L))))
> L
(list 1 2 (list 'a 3 4 5) (list 'car (list 'cdr 'L))) ;; oh no

But that still doesn't extract the 'a because you need to access car of the inner list: 但这仍然不能提取'a因为您需要访问内部列表的car

(define L '(1 2 (a 3 4 5)))
(car (car (cdr (cdr L))))
;; or (caaddr L)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM