[英]How do I access nested list in raket using car cdr caar caddr etc.?
I am trying to get familiar with accessing the nested list in Racket.我正在尝试熟悉访问 Racket 中的嵌套列表。 I have the following problem.
我有以下问题。 For example I have a list like this
define x (list 1 2 3 )
I understand that (car x) -> 1
and (cdr x) -> (2 3)
.例如,我有一个像这样
define x (list 1 2 3 )
的列表,我理解(car x) -> 1
和(cdr x) -> (2 3)
。 But if I have a list like this define y (list (list (list 6 7 8 ) 2 5 ) 3 4 5 6 ))
and I run (caaar y)
I get 6
.但是,如果我有一个这样的列表,请定义
y (list (list (list 6 7 8 ) 2 5 ) 3 4 5 6 ))
并运行(caaar y)
我得到6
。 I understand what is happening until now.我明白到目前为止发生的事情。 What I am not understanding is why do I get an error when I run
(caadr y)
.我不明白的是为什么我在运行
(caadr y)
时会出错。 (saying contract violation) instead of returning ( 7 8 )
. (说违反合同)而不是返回
( 7 8 )
。 Can you explain this to me please?你能给我解释一下吗?
(caadr y)
is (car (car (cdr y)))
, so you will get (car (car '(3 4 5 6)))
=> (car 3)
=> error
. (caadr y)
是(car (car (cdr y)))
,所以你会得到(car (car '(3 4 5 6)))
=> (car 3)
=> error
。
You need to return '(7 8)
, that is (cdr (car (car y)))
, so letters are taken in this order: C-cDr-cAr-cAr-R => CDAAR, (cdaar y)
.您需要返回
'(7 8)
,即(cdr (car (car y)))
,因此字母按以下顺序排列: C-cDr-cAr-cAr-R => CDAAR, (cdaar y)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.