質問

What's in the equivalent, in optima, of these two pattern matching examples - from On Lisp and PAIP, respectively?

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


(difference between ?x* and ?y*)
役に立ちましたか?

解決

The patterns in optima look as if they are building the form you are matching. So for example in this simple example:

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

The pattern is the form (list 'a 'b x) and that last form will print: c

Where as (match '(a b c d) ((list* 'a 'b x) (print x))) will print: (c d)

You maybe familiar with the sweet special syntax for building lists known as backquote. It is often used in defining macros were lots of s-expresssions are constructed. There is a add on package for optima that lets you write your patterns in the same manner.

(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))

Those last two forms will return c and (c) respectively.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top