Search code examples
lispschemesicp

When does scheme evaluate quote?


(car ''abracadabra) is equivalent to (car (quote (quote abracadabra)), and it evaluates to (car (quote abracadabra)) --> quote

On the othe hand (car (quote (a b))) evaluates to a, which to me makes sense intuitively.

So my question is, why does Scheme not evaluate the second quote in (car (quote (quote abracadabra)) (i.e. evaluate (car (quote abracadabra)) to (car abracadabra)), but does evaluade the quote in (car (quote (a b))) (i.e. why isn't the answer quote)?


Solution

  • In this expression:

    (car (quote (quote abracadabra)))
    => 'quote
    

    The inner quote doesn't get evaluated, it's just a symbol, with no particular meaning. You might as well change it for anything else, with the same results:

    (car (quote (foobar abracadabra)))
    => 'foobar
    

    Inside a quoted expression, other expressions won't get evaluated. We could use quasiquoting to force evaluation, now this will attempt to evaluate the inner quote, resulting in a different error for each case:

    (car (quasiquote (unquote (quote abracadabra))))  ; (car `,(quote abracadabra))
    => car: contract violation expected: pair? given: 'abracadabra
    
    (car (quasiquote (unquote (foobar abracadabra)))) ; (car `,(foobar abracadabra))
    => foobar: unbound identifier in module in: foobar