Lisp 球拍匹配语法准音符和问号

Lisp 球拍匹配语法准音符和问号,lisp,scheme,pattern-matching,racket,Lisp,Scheme,Pattern Matching,Racket,我试图理解racket的模式匹配文档,但有如下问题,我无法解析它 (准qp)-引入准模式,其中标识符匹配符号。与quasiquote表达式形式一样,unquote和unquote拼接返回到正常模式 例如: > (match '(1 2 3) [`(,1 ,a ,(? odd? b)) (list a b)]) '(2 3) 它没有解释这个例子,以及“标识符如何匹配符号”?我猜它是匹配模式的'(1,a,b),而b是奇数,但是为什么`(,1,a,(?奇数?b))不是`(1 a

我试图理解racket的模式匹配文档,但有如下问题,我无法解析它

  • (准qp)-引入准模式,其中标识符匹配符号。与quasiquote表达式形式一样,unquote和unquote拼接返回到正常模式

例如:

> (match '(1 2 3)
    [`(,1 ,a ,(? odd? b)) (list a b)])

'(2 3)
它没有解释这个例子,以及“标识符如何匹配符号”?我猜它是匹配模式的
'(1,a,b)
,而b是奇数,但是为什么
`(,1,a,(?奇数?b))
不是
`(1 a(?奇数?b))
,它在列表成员之间需要逗号呢?尤其是
`(,
?为什么会这样?所以字符串


谢谢!

如果您不熟悉quasiquoting,那么您可能需要熟悉
匹配中的
列出
模式,然后学习一般的quasiquoting。然后将两者结合起来会更容易理解

为什么?因为Quasikote“只是”一个简写或是你可以用
list
写的东西的替代品。虽然我不知道实际的开发历史,但我想
match
的作者一开始就有了
list
cons
struct
等模式。然后有人指出,“嘿,有时候我更喜欢用准量化描述
列表
”,他们也添加了准量化

#lang racket

(list 1 2 3)
; '(1 2 3)
'(1 2 3)
; '(1 2 3)

(define a 100)
;; With `list`, the value of `a` will be used:
(list 1 2 a)
; '(1 2 100)
;; With quasiquote, the value of `a` will be used:
`(1 2 ,a)
; '(1 2 100)
;; With plain quote, `a` will be treated as the symbol 'a:
'(1 2 a)
; '(1 2 a)

;; Using `list` pattern
(match '(1 2 3)
  [(list a b c) (values a b c)])
; 1 2 3

;; Using a quasiquote pattern that's equivalent:
(match '(1 2 3)
  [`(,a ,b ,c) (values a b c)])
; 1 2 3

;; Using a quote pattern doesn't work:
(match '(1 2 3)
  ['(a b c) (values a b c)])
; error: a b c are unbound identifiers

;; ...becuase that pattern matches a list of the symbols 'a 'b 'c
(match '(a b c)
  ['(a b c) #t])
; #t