简体   繁体   English

如何使用 car cdr caar caddr 等访问 raket 中的嵌套列表?

[英]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.

相关问题 方案:列表的CAR和CDR - Scheme: CAR and CDR of a list 如何仅使用 car、cdr、cons 和其他函数拆分列表 (python) - How to split a list only using car, cdr, cons and other functions (python) 如何在Scheme中使用Car和Cdr打破(11(12 13)) - How to break (11 (12 13)) using Car and Cdr in Scheme 在scheme / lisp中列出用car和cdr进行呼叫 - List calling with car and cdr in scheme/lisp 如何获得第二行(第三行等)的缩进,以使用CSS使用自定义编号匹配列表中的第一行? - How can I get second (third, etc.) line indentation to match first line in a list with custom numbering using CSS? 优先选择 rest 而不是 car 和 cdr 的 lisp 如何处理 cdaddr 之类的组合? - How do lisps that prefer first and rest to car and cdr approach combinations like cdaddr? Python 将打印列表值,但在尝试访问值时表示列表为空(使用 .pop() 或 indexing[0] 等) - Python will print list value but says list is empty when trying to access values (using .pop() or indexing[0] etc.) 宠物类型列表,宠物具有子类别Dog,Cat等。我如何知道它是哪个子类别? - List of type Pet, Pet has child classes Dog, Cat, etc. How do I tell which child class it is? 如何对列表中的汽车和干部? - How do I do a car and cadr against a list? 计划中的汽车和Cdr - Car and Cdr in Scheme
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM