Question

I know that when you type for example in a repl 'a, it will output a without evaluating it and internally, in my case, it expend to (##core#quote a) in Chicken-scheme.

I know that numbers and strings are special in a way that they are self-evaluating symbols. I guess that for that reason, the quote doesn't seem to have any effect on them.

For example:

(+ '1 '2 '3)
> 6
(+ (quote 1) (quote 2) (quote 3))
> 6

(string-append '"hello " '"world")
>"hello world"

But doing the following

''1
(quote 1) => (#core#quote '1) => (#core#quote (#core#quote 1))

If we do that:

(car ''a)
> quote

Which confirms what I tought. Then if we do the following we should find 1 as expected.

(cadr ''1)
> 1

Am I right that quoted self evaluating identifiers are ignored at evaluation time? Because if I do

(define a '1)
a

It doesn't print '1 but 1.

Was it helpful?

Solution

What you should try to do here is to understand how evaluation works by writing a very simple evaluator. For example (and this is pseudocode!):

(define (self-evaluating? form)
  ;; This might not be exactly right, might be missing one type or two,
  ;; but you get the idea.
  (or (null? form)
      (number? form) 
      (boolean? form) 
      (string? form) 
      (vector? form)
      (character? form)))

(define (eval form env)
  (cond ((self-evaluating? form)
         ;; Self-evaluating forms evaluate to themselves. 
         form)
        ((symbol? form)
         ;; A symbol is evaluating by looking it up in the environment.
         ;; Note that this is pseudocode, and lookup-in-environment is not
         ;; a standard function... 
         (lookup-in-environment form env))
        ((list? form)
         (eval-combination form env))))

(define (eval-combination form env)
  (case (car form)
    ((quote)
     ;; A quote special form is evaluated simply by returning the 
     ;; argument to quote.
     (second form))

    ((define)
     ;; We evaluate a definition by evaluating the body in the current
     ;; environment, and setting the variable to the result in that 
     ;; environment.
     ;;
     ;; Note again that this is pseudocode, and set-in-environment! is
     ;; not a standard function... 
     (set-in-environment! env (second form) (eval (third form) env)))

    ;; Other special forms
    ...

    ;; Default rule: evaluate all the subexpressions in the current 
    ;; environment; the first one should be a procedure, which we then
    ;; apply to the list of values of the succeeding ones.
    (else
     (apply (eval (car form) env) 
            (map (lambda (arg) (eval arg env)) (cdr form)))))))

By tracing the execution of that code by hand for a few examples (and you can ignore the env parameter for your case), you should be able to see what's going on. The key thing to note about an evaluator like this is that it's very orthogonal: for each type of form there is a separate rule on how to evaluate it, and the rules don't know about each other. It's all driven by this logic:

  1. Is the form an atom or a combination (list)?
  2. If the form is an atom, is it self-evaluating or is it a symbol?
  3. If the form is a combination, is it a special form or a procedure application? The only thing we look at to decide this is the form's car.

OTHER TIPS

You're right. Evaluating (quote <object>) returns that object. Evaluating a self-evaluating object also returns that object.

You're thinking of it backwards. It's not that quote is ignored when the object is self-evaluating, it's that self-evaluating objects effectively quote themselves.

Syntactic keywords define their own evaluation rules. For example:

(define <identifier> <inititailizer>)

does not evaluate <identifier> but does evaluate <initializer>. The syntactic keyword quote does not evaluate its argument but when quote itself is evaluated it returns its argument. So if you write:

(define a '1)

the '1 is evaluated which evaluates the quote syntax to the number 1.

Note that self-evaluating expressions are defined in R7RS as:

⟨self-evaluating⟩ −→ ⟨boolean⟩ | ⟨number⟩ | ⟨vector⟩
  | ⟨character⟩ | ⟨string⟩ | ⟨byte vector⟩
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top