Alf*_*ong 2 lisp scheme pattern-matching racket
我正在尝试了解球拍的图案匹配文件,并且遇到一些类似以下的问题,我无法解析它。
http://docs.racket-lang.org/reference/match.html
例:
> (match '(1 2 3)
[`(,1 ,a ,(? odd? b)) (list a b)])
'(2 3)
Run Code Online (Sandbox Code Playgroud)
它没有解释此示例,“标识符与符号匹配”如何?我猜它与'(1 2 3)模式匹配'(1, a, b)并且b是奇数,但是为什么`(,1 ,a ,(? odd? b))不呢`(1 a (? odd? b)),它在列表成员之间需要逗号吗?特别是`(,?为什么这样呢?真弦!
谢谢!
如果您对准引用不熟悉,那么您可能会熟悉中的list模式match,然后大致了解准引用。然后将两者放在一起会更容易理解。
为什么?因为quasiquote只是您可以使用编写的内容的简写或替代形式list。虽然我不知道实际的发展历史,我想象的作者(S)match开始了与喜欢的图案list,cons,struct等等。然后有人指出:“嘿,有时候我更喜欢描述list使用准引用”,他们也添加了准引用。
#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
Run Code Online (Sandbox Code Playgroud)