listlispcommon-lispcdr

Lisp list manipulation issue


I have this expression ,

(write (cdr (car' ('(p q) r))))

http://ideone.com/bkZv20

which gives ((P Q)) as the output . I've been scratching my head all day and am still unable to figure out how this works .

Doing just the car part gives,

(write (car' ('(p q) r)))

gives '(P Q) .

Then , according to me (cdr '(P Q)) should give (Q) as the output .

How is the final answer , '(P Q) is my question .


Solution

  • You have an extra quote (the first one is stuck to the car but still parses correctly) in there which causes a quoted quote, so what you essentially have is:

    (write (cdr (car '((quote (p q)) r))))
    

    Taking the car of this leaves you with just the data:

    (quote (p q))
    

    And again taking the cdr of this results in the data:

    (p q)
    

    As you observed. If you look at the car of the car instead with

    (write (car (car '((quote (p q)) r))))
    

    you should see the

    quote
    

    itself. Remember that '(a b) and (quote (a b)) are the same thing, and the printout from whatever you're using might show either form.

    So what you want to do is just remove the extra quote, i.e.:

    (write (cdr (car '((p q) r))))