简体   繁体   English

为什么在Racket语法中允许使用“点对”s表达式?

[英]Why are “dotted pair” s-expressions allowed in Racket syntax?

After more or less understanding the answers to this question , it looks to me that in Racket/Scheme, at the reader level, the second element of each pair in the syntax tree has to be a list. 在或多或少地理解了这个问题的答案之后,我认为在Racket / Scheme中,在读者级别,语法树中每对的第二个元素必须是一个列表。 In other words, whenever a dotted s-expression of the form (A . B) represents a vertex of the syntax tree, B can only by an s-expression that parses as a list, like (CDE) . 换句话说,每当形式(A . B) A.B)的虚线s表达式表示语法树的顶点时, B只能通过作为列表解析的s表达式,如(CDE) For example: (A . (CDE)) . 例如: (A . (CDE)) This of course can be written as (ACDE) , because it is parsed identically. 这当然可以写成(ACDE) ,因为它被解析相同。

(+ . (1 2 3)) ; => 6
(+ 1 2 3) ; => 6

(define . (x 1))
x ; => 1
(define y 2)
y ; => 2

My question is: what is the reason that "dotted pair" s-expressions are allowed in the Racket/Scheme syntax, other than inside literal data? 我的问题是:除了文字数据之外,在Racket / Scheme语法中允许使用“点对”s表达式的原因是什么? Is there an example of a Racket/Scheme expression that can be written using pairs, but cannot be written simpler using lists? 是否有可以使用对编写的Racket / Scheme表达式的示例,但使用列表不能更简单地编写?

In any Lisp system, reading and evaluating are separate steps. 在任何Lisp系统中,阅读和评估都是单独的步骤。 To the reader, everything is literal data; 对读者来说, 一切都是文字数据; it's the evaluator that decides what to evaluate and what (by virtue of quote and quasiquote ) to treat as literal data. 它是评估者,决定评估什么以及(通过quotequasiquote )将其视为文字数据。

The reader reads the following expressions exactly the same way: 读者完全以相同的方式阅读以下表达式:

(+ 1 2 3)
(+ . (1 2 3))
(+ . (1 . (2 3)))
(+ . (1 . (2 . (3))))
(+ . (1 . (2 . (3 . ()))))

This is because, at the basic level, non-empty lists are made up of a bunch of cons cells, which happen to have a cdr that points to another list (empty or not). 这是因为,在基本级别,非空列表由一堆cons单元组成,这些单元恰好有一个指向另一个列表的cdr (空或不)。

Furthermore, there are legitimate Scheme expressions that do use improper lists. 此外,也合法的方案表述,确实使用了不正当的名单。 Rest arguments for lambdas are a prime example of this: lambdas的rest参数就是一个很好的例子:

(define (list . items)
  items)

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

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