0

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 .

Machavity
  • 30,841
  • 27
  • 92
  • 100
saruftw
  • 1,104
  • 2
  • 14
  • 37
  • What is this mystery `car'` function you seem to be using? Do you really mean to use `'` to quote twice? – Matti Virkkunen Feb 23 '16 at 22:58
  • I recieved the expression that way ! Have a look at the link , – saruftw Feb 23 '16 at 22:59
  • I think you might find the answers to [Replace elements in nested quoted lists adds new elements?](http://stackoverflow.com/questions/24370500/replace-elements-in-nested-quoted-lists-adds-new-elements) helpful. – Joshua Taylor Feb 24 '16 at 13:56

1 Answers1

6

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))))
Matti Virkkunen
  • 63,558
  • 9
  • 127
  • 159