2014-02-07 23 views
0

我知道,例如在repl 'a中输入时,它会输出a而不对其进行评估,在我的情况下,它会在Chicken-scheme中花费到(##core#quote a)报价是否忽略了自我评估标识符?

我知道数字和字符串是特殊的,它们是自我评估的符号。我想那个是因为这个原因,quote似乎对他们没有任何影响。

例如:

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

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

但这样做以下

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

如果我们这样做:

(car ''a) 
> quote 

这证实了我因子评分。然后,如果我们做了以下,我们应该找到1预期。

(cadr ''1) 
> 1 

我说得对不对,所列的自我评价标识在评估时间被忽略?因为如果我这样做

(define a '1) 
a 

它不打印'1但1

回答

3

你应该尝试做的,是要了解如何评价工作通过编写一个非常简单的评价。例如(这是伪代码!):

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

通过手动跟踪代码的执行了几个例子(你可以忽略你的情况env参数),你应该能够看到什么继续。关于这样的评估者关键的一点是,它非常正确正交:对于每种形式,有一个关于如何评估它的单独规则,并且规则不相互了解。它都是由这个逻辑驱动的:

  1. 表单是原子还是组合(列表)?
  2. 如果形式是一个原子,它是自我评估还是符号?
  3. 如果表单是一个组合,它是一种特殊的表单还是一个过程应用程序?我们唯一要看的是表单的car
+0

这样做很有意义,因为我们只有列表,所以在应用参数之前,我们总是可以检查列表中的第一个符号。在引用的情况下,我们只需返回数字,如果我们有一个数字,我们只是返回数字,但如果我们有一个引用的数字,我们只会返回带有引用数字的列表。 –

2

你说得对。评估(quote <object>)返回该对象。评估自我评估对象也会返回该对象。

你在想它。当对象是自我评估时,并不是说引用被忽略,而是自我评估对象有效地引用了自己。

2

句法关键字定义了自己的评估规则。例如:

(define <identifier> <inititailizer>) 

不评估<identifier>但评估<initializer>。语法关键字quote不评估它的参数,但当quote本身被评估时,它返回它的参数。所以,如果你写:

(define a '1) 

'1评估其评估quote语法the number 1

注意自我评估表达式在R7RS定义为:

⟨self-evaluating⟩ −→ ⟨boolean⟩ | ⟨number⟩ | ⟨vector⟩ 
    | ⟨character⟩ | ⟨string⟩ | ⟨byte vector⟩