Search code examples
schemelispcdr

Scheme - cdr without outer brackets


consider this list: ((3 (1 (2 15) (8 5))) (1 10)), I would like to get its head and body. The procedure "car" works here as I imagine: (car '((3 (1 (2 15) (8 5))) (1 10))) - return its first item (list)

(3 (1 (2 15) (8 5)))

But procedure "cdr: returns list containing another list ... (cdr '((3 (1 (2 15) (8 5))) (1 10)))

((1 10))

I would like to have (1 10) instead of it. Does Scheme has some procedure for it or you know some way to reach it (like to check whether first two characters and last two characters are brackets)?

Thank you all:)


Solution

  • Basically you have car, cadr, caddr, ... to get the first, second, third element, ...

    A list is a linked list where cons has a value in it's car and the rest of the list in it's cdr. '(a b c d) is the same as '(a . (b . (c . (d)))) and you can make it with (cons a (cons b (cons c (cons d '())))). cdr of it would be (b . (c . (d))) and car of that again would be b. Thus (car (cdr '(a . (b . (c . (d)))))) is b. Scheme has made abbriviations of 2..5 such so you just look at the middle char and knwo its the same as (cadr '(a . (b . (c . (d)))))