将?x样式模式变量转换为最佳值

BnM*_*cGn 2 common-lisp pattern-matching

这两个模式匹配示例中的最佳等价物是什么- 分别来自On Lisp和PAIP?

>(match ’(p a b c a) ’(p ?x ?y c ?x))
((?Y . B) (?X . A))
T


(difference between ?x* and ?y*)
Run Code Online (Sandbox Code Playgroud)

Ben*_*yde 6

最佳模式看起来好像正在构建您匹配的形式.例如,在这个简单的例子中:

(ql:quickload "optima")
(defpackage #:example (:use #:common-lisp #:optima))
(in-package #:example)
(match '(a b c) ((list 'a 'b X) (print X)))
Run Code Online (Sandbox Code Playgroud)

模式是表单(list 'a 'b x),最后一个表单将打印:c

在哪里(match '(a b c d) ((list* 'a 'b x) (print x)))打印:(c d)

您可能熟悉构建列表(称为反引号)的甜蜜特殊语法.它通常用于定义宏,构造了许多s-expresssions.有一个针对optima的附加包,可让您以相同的方式编写模式.

(ql:quickload '("fare-quasiquote-optima" "fare-quasiquote-readtable"))
(named-readtables:in-readtable :fare-quasiquote)

(match '(a b c) (`(a b ,x) x))
(match '(a b c) (`(a b ,@x) x))
Run Code Online (Sandbox Code Playgroud)

这些后两种形式将返回c(c)分别.