Following is a exercise from SICP. I couldn't figure it out on my own. Can some why help me understand?
Type following code into interpreator:
(car ''abracadabra)
And it print out 'quote'. Why?
Following is a exercise from SICP. I couldn't figure it out on my own. Can some why help me understand?
Type following code into interpreator:
(car ''abracadabra)
And it print out 'quote'. Why?
As gimpf said, 'abracadabra = (quote abracadabra). You can verify this by typing ''abracadabra to the REPL, which will print (quote abracadabra).
Because ''abracadabra is really (quote (quote abracadabra)). In Scheme, the rule is: evaluate all the parts of the s-expression, and apply the first part to the rest of the parts.
"car" and "quote" are symbols in the below. #car and #quote are the functions they refer to.
If you take
(car (quote (quote abracadabra)))
and evaluate the parts, you get
(#car (quote abracadabra))
Then, apply the first part (the car function) to the second part (a list of two symbols).
quote
And you get just the symbol "quote".
Just remember, to figure out what happens in Scheme, evaluate the parts and apply the first to the rest. If you evaluate quote, you get the stuff inside. The only confusing part is that some primitives (number and strings) evaluate to themselves.