Clojure简单模式匹配

时间:2011-06-14 18:19:18

标签: macros clojure pattern-matching

作为previous question的后续内容,我正在尝试在Clojure中实现一个简单的模式匹配。

我想要以下内容:

(match target
  [ub]    expr1    ; ub should be bound to actual value in expr1
  ['< ub] expr2    ; match the literal less-than symbol
                   ; and ub should be bound to actual value in expr2
  [lb ub] expr3    ; lb and ub should be bound to actual values in expr3
  :else   expr4    ; default case if none match
)

用法:

(match [< 5.0] ...)

应安排在运行时执行expr2

我想写一个宏,但我不确定扩展。

我正在考虑将每个case-and-clause扩展为let并绑定到内部变量,并检查文字符号('<)是否与模式匹配。也许是第二种模式(['< ub]):

(let [[sym1 ub] pattern]
  (if (= '< sym1)
    expr1)

我是否需要使用(gensym)进行绑定?怎么样?

更大的图片:

(range-case target
            [0.0 < 1.0] :greatly-disagree
            [< 2.0]     :disagree
            [< 3.0]     :neutral
            [< 4.0]     :agree
            [5.0]       :strongly-agree
            42          :the-answer
            :else       :do-not-care)

我正在尝试匹配[...]模式并将它们转换为以下内容:

[ub]          (if previous-ub `(and (<= ~previous-ub ~target) (<= ~target ~ub))
                              `(< ~target ~ub))
['< ub]       (if previous-ub `(and (<= ~previous-ub ~target) (< ~target ~ub))
                              `(< ~target ~ub))
[lb ub]       `(and (<= ~lb ~target) (<= ~target ~ub))
['< lb ub]    `(and (< ~lb ~target) (<= ~target ~ub))
[lb '< ub]    `(and (<= ~lb ~target) (< ~target ~ub))
['< lb '< ub] `(and (< ~lb ~target) (< ~target ~ub))

我有一个cond来检查案例部分是一个向量。这种模式匹配应该发生在这种情况下。

1 个答案:

答案 0 :(得分:3)

我的第一个想法基本相同:将内容绑定到内部本地人并在一个大的and中测试他们的内容。对于文字,值绑定到生成的本地;符号直接用于绑定。

我还添加了一个检查,即spec矢量与目标矢量的长度匹配。否则,您不能拥有[ub]以及[lb ub],因为它们都不包含可能失败的支票。所以总是先选择第一个。

以下是代码:

(defn make-clause
  [expr-g [spec expr & more :as clause]]
  (when (seq clause)
    (let [tests-and-bindings (map (fn [x]
                                    (if-not (symbol? x)
                                      (let [x-g (gensym "x")]
                                        [`(= ~x ~x-g) x-g])
                                      [nil x]))
                                  spec)
          tests    (keep first tests-and-bindings)
          bindings (map second tests-and-bindings)]
      `(let [[~@bindings] ~expr-g]
         (if (and (= (count ~expr-g) ~(count spec)) ~@tests)
           ~expr
           ~(make-clause expr-g more))))))

(defmacro match
  [expr & clauses]
  (let [expr-g  (gensym "expr")]
    `(let ~[expr-g expr]
       ~(make-clause expr-g clauses))))

并举例说明。我没有在示例中使用syntax-quote来减少扩展中的噪音,但是你应该明白这一点。

(let [expr98 [(quote <) 3.0]]
  (let [[ub] expr98]
    (if (and (= (count expr98) 1))
      (if previous-ub
        (and (<= previous-ub target) (<= target ub))
        (< target ub))
      (let [[x99 ub] expr98]
        (if (and (= (count expr98) 2) (= (quote <) x99))
          (if previous-ub
            (and (<= previous-ub target) (< target ub))
            (< target ub))
          (let [[lb ub] expr98]
            (if (and (= (count expr98) 2))
              (and (<= lb target) (<= target ub))
              nil)))))))

邀请函是:

(match ['< 3.0]
  [ub]    (if previous-ub
            (and (<= previous-ub target) (<= target ub))
            (< target ub))
  ['< ub] (if previous-ub
            (and (<= previous-ub target) (< target ub))
            (< target ub))
  [lb ub] (and (<= lb target) (<= target ub))))

希望能帮助您入门。