简体   繁体   中英

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. 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) . 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 . I understand what is happening until now. What I am not understanding is why do I get an error when I run (caadr y) . (saying contract violation) instead of returning ( 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 .

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) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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