定义匹配扩展器

发布于 2024-10-19 11:35:04 字数 483 浏览 2 评论 0原文

关于定义匹配扩展,有很少的材料和示例代码来说明概念。我很难“解码”文档所说的内容:

(define-match-expander id proc-expr)
(define-match-expander id proc-expr proc-expr)

将 id 绑定到匹配扩展器。

第一个 proc-expr 子表达式必须 评估变压器 产生匹配的拍拍。每当 ID 显示为模式的开头, 该变压器给出,在 扩展时间,语法对象 对应整个图案 (包括 ID)。图案是 替换为结果 变压器。

第二个生产的变压器 proc-expr 子表达式用于以下情况 id 在表达式上下文中使用。 使用第二个 proc-expr,id 可以是 赋予内在和外在的意义 模式。

谁能给出一些示例代码来说明这里定义匹配扩展器的两种用法?

about the define-match-expansion, there are rare materials and example codes to illustrate the concepts. I am having a hard time to "decode" what the documentation says:

(define-match-expander id proc-expr)
(define-match-expander id proc-expr proc-expr)

Binds id to a match expander.

The first proc-expr subexpression must
evaluate to a transformer that
produces a pat for match. Whenever id
appears as the beginning of a pattern,
this transformer is given, at
expansion time, a syntax object
corresponding to the entire pattern
(including id). The pattern is the
replaced with the result of the
transformer.

A transformer produced by a second
proc-expr subexpression is used when
id is used in an expression context.
Using the second proc-expr, id can be
given meaning both inside and outside
patterns.

Can anyone give some example codes to illustrate the two usages of the define-match-expander here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玩世 2024-10-26 11:35:04

匹配扩展器背后的想法是,您可以扩展“匹配”形式来处理您自己设计的新模式形式。

因此,这里有一个(有点毫无意义的)示例,它定义了一个“aba”匹配形式,该形式匹配一个事物的模式,后跟另一个事物,然后再次是第一个事物(因此,“aba”):

#lang racket

(define-match-expander aba
  (lambda (stx)
    (syntax-case stx ()
      [(_ a b) #'(list a b a)])))

(match '(3 4 3)
  [(aba x y) (printf "x = ~a, y = ~a" x y)])

第二种形式允许您添加单独的在匹配模式之外使用的扩展,如下所示:

#lang racket

(define-match-expander aba
  (lambda (stx)
    (syntax-case stx ()
      [(_ a b) #'(list a b a)]))
  (lambda (stx)
    #'(error "please don't use aba outside of patterns.")))

(match '(3 4 3)
  [(aba x y) (printf "x = ~a, y = ~a\n" x y)])

(aba x y)

警告:模式周围有一对额外的括号?不确定,抱歉。

The idea behind match-expander is that you can extend the 'match' form to handle new pattern forms of your own design.

So, here's a (somewhat pointless) example that defines an "aba" match form that matches patterns of one thing followed by another thing followed by the first thing again (hence, "aba"):

#lang racket

(define-match-expander aba
  (lambda (stx)
    (syntax-case stx ()
      [(_ a b) #'(list a b a)])))

(match '(3 4 3)
  [(aba x y) (printf "x = ~a, y = ~a" x y)])

The second form allows you to add a separate expansion to be used outside of match patterns, like this:

#lang racket

(define-match-expander aba
  (lambda (stx)
    (syntax-case stx ()
      [(_ a b) #'(list a b a)]))
  (lambda (stx)
    #'(error "please don't use aba outside of patterns.")))

(match '(3 4 3)
  [(aba x y) (printf "x = ~a, y = ~a\n" x y)])

(aba x y)

Caveat: whuffo the extra pair of parens around the pattern? Not sure, sorry.

和影子一齐双人舞 2024-10-26 11:35:04

这是一个非常老的问题,但我想为相同的“aba”模式添加使用“syntax-rules”的示例:

(define-match-expander aba
  (syntax-rules ()
    [(aba a b) (list a b a)]))

(match '(3 4 3)
  [(aba x y) (printf "x = ~a, y = ~a" x y)])

左侧 (aba ab) 是要输入的内容match,右侧(list aba)是替换。

(match '(3 4 3)
  [(aba x y) (printf "x = ~a, y = ~a" x y)])

替换为

(match '(3 4 3)
  [(list x y x) (printf "x = ~a, y = ~a" x y)])

好的部分是新的匹配器适用于所有“匹配任何”功能,例如:

(match-define (aba x y) (list 1 2 1))
(printf "x = ~a, y = ~a" x y

This is a really old question, but I'd like to add the example using "syntax-rules" for the same "aba" pattern:

(define-match-expander aba
  (syntax-rules ()
    [(aba a b) (list a b a)]))

(match '(3 4 3)
  [(aba x y) (printf "x = ~a, y = ~a" x y)])

Left-hand side (aba a b) is the thing going in the match, the right-hand side (list a b a) is the substitution.

(match '(3 4 3)
  [(aba x y) (printf "x = ~a, y = ~a" x y)])

is replaced by

(match '(3 4 3)
  [(list x y x) (printf "x = ~a, y = ~a" x y)])

The nice part is that the new matcher works for ALL "match-whatever" functions, like:

(match-define (aba x y) (list 1 2 1))
(printf "x = ~a, y = ~a" x y
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文