简体   繁体   English

为什么符号pat…表示方案宏模式中的零个或多个表达式

[英]why notation pat … means zero or more expressions in scheme macro pattern

When I'm reading the macro part of The Scheme Programming Language , it mentions that when you are trying to define a syntax extension using define-syntax , you should use pat ... to specify zero or more expression in the pattern. 当我阅读The Scheme Programming Language的宏部分时 ,它提到当您尝试使用define-syntax定义语法扩展时,应使用pat ...在模式中指定零个或多个表达式。 Why don't just use ... for zero or more expression, in which case pat ... means one or more expressions? 为什么不只将...用于零个或多个表达式,在这种情况下pat ...表示一个或多个表达式?

Also, when the author gives definition of and as follows: 另外,当作者给出and定义时and如下:

(define-syntax and
  (syntax-rules ()
    [(_) #t]
    [(_ e) e]
    [(_ e1 e2 e3 ...)
     (if e1 (and e2 e3 ...) #f)]))

Why don't just write it like this: 为什么不这样写:

(define-syntax and
  (syntax-rules ()
    [(_) #t]
    [(_ e) e]
    [(_ e1 e2 ...)
     (if e1 (and e2 ...) #f)]))

I have tested this definition with some cases, and I didn't find any problem with it. 我已经在某些情况下测试了此定义,但没有发现任何问题。

Per R6RS , ... must always follow some identifier to get its "zero or more" meaning (it behaves like the Kleene star ), so ... and pat ... mean different things: the former is just a literal ... symbol as far as syntax-rules is concerned. 对于R6RS...必须始终跟随某些标识符以获取其“零个或多个”含义(其行为类似于Kleene星 ),因此...pat ...表示不同的含义:前者只是一个文字...符号至于syntax-rules而言。

In your example, both forms seem to be the same, but that's because the pattern (_ e1 e2 ...) only fires when there are two or more conjuncts (arguments to and ) anyway, since the second pattern (_ e) already handles the case where there's only one conjunct. 在您的示例中,两种形式似乎是相同的,但这是因为模式(_ e1 e2 ...)仅在存在两个或多个合词( and参数(_ e1 e2 ...)才会触发,因为第二个模式(_ e)已经存在处理只有一个合相的情况。 It doesn't always work this way, and Dybvig apparently found it cleaner to write an explicit "two or more" clause rather than "exactly one" and "one or more" clauses. 它并不总是以这种方式工作,并且Dybvig显然发现编写一个显式的“两个或多个”子句比“精确地一个”和“一个或多个”子句更干净。

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

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