Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does scheme evaluate quote?

Tags:

lisp

scheme

sicp

(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)?

like image 739
Sebastian Mendez Avatar asked Jun 02 '13 16:06

Sebastian Mendez


3 Answers

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
like image 151
Óscar López Avatar answered Sep 22 '22 06:09

Óscar López


Scheme doesn't evaluate the inner quote because the inner quote is inside the outer quote and expressions inside a quote aren't evaluated. That is if you write (quote foo), then foo will never be evaluated - even if foo is another call to quote.

like image 24
sepp2k Avatar answered Sep 22 '22 06:09

sepp2k


When scheme evaluates a combination it evaluates the operator. Then it will apply if it's a special form or macro. If not, it will evaluate each operands before applying.

(car (quote (quote abracadabra)))

Can be evaluated like this*

  • car evaluates to #<proc car>. since it's a procedure evaluate all operands in any order
  • (quote (quote abracadabra)) is a combination, thus
    • quote is evaluated => #<macro quote> and since its a macro/special form do macro-apply
    • apply-macro (#<macro quote> (quote abracadabra))) => (quote abracadabra)
  • apply-proc (#<proc car> (quote abracadabra)) => quote

*Actually Scheme can shadow any keyword so the context is very important. E.g.:

(let ((quote list) (abracadabra 'simsalabim))
  (car (quote (quote abracadabra)))) 
==> (simsalabim)
like image 22
Sylwester Avatar answered Sep 22 '22 06:09

Sylwester