简体   繁体   English

Racket 中的 '(撇号)是什么?

[英]What is ' (apostrophe) in Racket?

I am a little confused about the meaning of the ' sign in racket.我对球拍中的'标志'的含义有些困惑。 It appears to me that the same sign has different meanings.在我看来,同一个符号有不同的含义。 Look at 2 simple examples below:看下面两个简单的例子:

list

Returns a newly allocated list containing the vs as its elements.返回一个新分配的列表,其中包含 vs 作为其元素。

> (list 1 2 3 4)
'(1 2 3 4)

quote

Produces a constant value corresponding to datum (ie, the representation of the program fragment) without its lexical information, source location, etc. Quoted pairs, vectors, and boxes are immutable.产生与数据(即程序片段的表示)相对应的常量值,没有其词法信息、源位置等。引用对、向量和框是不可变的。

> '(1 2 3 4)
'(1 2 3 4)

So my question is: Does the ' sign has 2 meanings (a symbol and a list ) or are these the same data type and list actually returns a quoted constant value ?所以我的问题是: '符号是否有 2 个含义(一个符号和一个列表),或者这些是相同的数据类型并且list实际上返回一个带引号的常量值 If the second is the case why does this work:如果是第二种情况,为什么这样做:

> '(+ (- 2 13) 11)
'(+ (- 2 13) 11)

> (eval (list + (- 2 13) 11))
0

(also (eval '(+ (- 2 13) 11)) works and evaluates correctly to 0 ) (也(eval '(+ (- 2 13) 11))工作并正确评估为0

But this does not:但这不会:

> (list + (- 2 13) 11)
'(#<procedure:+> -11 11)

> (eval '(#<procedure:+> -11 11))
. read: bad syntax `#<'

Related maybe: What is ' (apostrophe) in Lisp / Scheme?相关可能: Lisp/Scheme 中的 '(撇号)是什么?

You are confused by the default way of #lang racket of printing values, which differs from almost all the other interactive lisp environments.您对#lang racket打印值的默认方式感到困惑,它与几乎所有其他交互式 lisp 环境不同。 If you choose in DrRacket itself another language, for instance R5RS, you will discover that it prints:如果你在 DrRacket 中选择另一种语言,例如 R5RS,你会发现它打印:

> (list 1 2 3 4)
(1 2 3 4)

that is, the result of the operator list applied to the numbers 1 2 3 4 is to produce a list of those numbers, which is exactly the interpretation of (1 2 3 4) .也就是说,运算符list应用于数字 1 2 3 4 的结果是生成这些数字的列表,这正是(1 2 3 4)的解释。

So, the answer to What is ' (apostrophe) in Lisp / Scheme?那么, Lisp/Scheme 中什么是 '(撇号)的答案是什么? is valid also for your case, and 'anything is just an abbreviation for (quote anything) .也适用于您的情况,并且'anything只是(quote anything)的缩写。

> is a sign of REPL - the Read-Eval-Print Loop. >是 REPL 的标志 - Read-Eval-Print Loop。

First, whatever expression you've typed at the REPL prompt is read - converted to some internal abstract syntax tree representation.首先,读取您在 REPL 提示符下键入的任何表达式 - 转换为某些内部抽象语法树表示。 Then this internal representation of the typed-in expression is evaluated - ie its value is found.然后键入的表达式求值的这个内部表示-即,其值被发现。 Then the result is printed.然后打印结果。

When we type当我们打字

> (list 1 2 3 4)

the typed-in expression is read as a nested structure, let's write it as输入的表达式被读取为嵌套结构,让我们将其写为

[LIST | [1 | [2 | [3 | [4 | NIL ]]]]]

according to the usual representation of lists as pairs of data and the rest of the list (here showing a pair of a and b as [a | b] ).根据列表作为数据对和列表其余部分的通常表示(此处将ab对显示为[a | b] )。

Then the above structure is evaluated, and because its first element was LIST it causes the invocation of list with the arguments as specified, which causes a fresh list structure to be built, which can be represented as然后对上面的结构求值,因为它的第一个元素是LIST它会导致调用带有指定参数的list ,这会导致构建一个新的列表结构,它可以表示为

[1 | [2 | [3 | [4 | NIL ]]]]

Then it is printed, usually as (1 2 3 4) but Racket chooses to print it as '(1 2 3 4) .然后它被打印出来,通常是(1 2 3 4)但 Racket 选择将它打印为'(1 2 3 4) Incidentally, it can't be evaluated, because 1 can not be called .顺便说一下,它不能被评估,因为1不能被调用

Next, the quoted expression '(1 2 3 4) , which is read as (quote (1 2 3 4)) .接下来,引用表达式'(1 2 3 4) ,读作(quote (1 2 3 4)) It is converted into它被转换为

[QUOTE | [ [1 | [2 | [3 | [4 | NIL ]]]] | NIL ]]

which, when evaluated (according to the evaluation rule for quote ), returns the data it received.它在评估时(根据quote的评估规则)返回它收到的数据 Which we represent as我们表示为

[1 | [2 | [3 | [4 | NIL ]]]]

That's why the two are similar.这就是为什么两者是相似的。 Whether we build a new list containing 1, 2, 3, and 4;我们是否构建一个包含 1、2、3 和 4 的新列表; or we cause it to be created as part of read process, so it gets returned verbatim by quote ;或者我们让它作为读取过程的一部分被创建,所以它会被quote逐字返回; the result is the same.结果是一样的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM