Racket中的'(撇号)是什么? [英] What is ' (apostrophe) in Racket?
问题描述
我对球拍中'
标志的含义有点困惑。在我看来,同一个符号有不同的含义。看看下面的两个简单例子:
返回一个新分配的列表,其中包含vs作为其元素。
> (list 1 2 3 4)
'(1 2 3 4)
产生一个对应于没有它的词汇信息,源位置等,引用的对,矢量和框是不可变的。
> '(1 2 3 4)
'(1 2 3 4)
是:
'
符号是否有2个含义(a 符号和列表)或是相同的数据类型和 list
实际上会返回一个引用的常量值?如果第二种情况是这种情况,为什么这么做:
> '(+( - 2 13)11)
'(+( - 2 13)11)
> (eval(list +( - 2 13)11))
0
code>(eval'(+( - 2 13)11))正常工作并正确评估为 0
)
但这并不是:
> (list +( - 2 13)11)
'(#< procedure:+> -11 11)
> (eval'(#< procedure:+> -11 11))
。阅读:坏语法`#<'
>
是REPL的标志 - Read-Eval-Print Loop。
首先,您在REPL提示符处输入的任何表达式都是 read - 转换为某种内部抽象语法树表示形式。然后,这个输入表达式的内部表示被评估 - 即找到它的值。然后打印结果。
当我们输入
> (list 1 2 3 4)
输入表达式被读为嵌套结构,让我们写它作为
[LIST | [1 | [2 | [3 | [4 | NIL]]]]]
根据数据对的列表的常见表示形式, (这里显示一对 a
和 b
为 [a | b]
)。
然后上面的结构被评估,因为它的第一个元素是 LIST
它会调用带有指定参数的 list
,这会导致建立一个新的列表结构,它可以表示为
[1 | [2 | [3 | [4 | NIL]]]]
然后打印它,通常为(1 2 3 4)
但球拍选择打印它为'(1 2 3 4)
。顺便说一句,它不能被评估,因为 1
不能被称为。
接下来,引用的表达式'(1 2 3 4)
,它被读作(quote(1 2 3 4))
。它被转换为
[QUOTE | [[1 | [2 | [3 | [4 |无]]]] NIL]]
当评估时(根据报价的评估规则
),返回它收到的数据。我们表示为
[1 | [2 | [3 | [4 | NIL]]]]
这就是两者相似的原因。我们是否建立一个包含1,2,3和4的新列表;或者我们将它创建为读取过程的一部分,所以它被逐字返回 quote
;结果是一样的。
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:
Returns a newly allocated list containing the vs as its elements.
> (list 1 2 3 4)
'(1 2 3 4)
Produces a constant value corresponding to datum (i.e., 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? 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
)
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?
>
is a sign of REPL - the Read-Eval-Print Loop.
First, whatever expression you've typed at the REPL prompt is read - converted to some internal abstract syntax tree representation. Then this internal representation of the typed-in expression is evaluated - i.e. 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]
).
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
[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)
. Incidentally, it can't be evaluated, because 1
can not be called.
Next, the quoted expression '(1 2 3 4)
, which is read as (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. 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; or we cause it to be created as part of read process, so it gets returned verbatim by quote
; the result is the same.
这篇关于Racket中的'(撇号)是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!