matche

added

ns
clojure.core.logic

type
macro

(matche xs & cs)

Pattern matching macro. All patterns will be tried.
See conde.

                ;; Taken from: https://github.com/frenchy64/Logic-Starter/wiki#matche
;; These are equivalent:
(run* [q]
      (conde
        ((== 'extra q) succeed)
        ((== 'virgin q) succeed)
        ((== 'olive q) succeed)
        ((== 'oil q) succeed)))
;=> (extra virgin olive oil)

(run* [q]
      (matche [q]
              (['extra]  succeed)
              (['virgin] succeed)
              (['olive]  succeed)
              (['oil]    succeed)))
;=> (extra virgin olive oil)

;; Wild Cards and destructuring: https://github.com/frenchy64/Logic-Starter/wiki#matche-sugar-combining-wildcards-and-destructuring
(run* [q]
  (fresh [a o]
    (== a [1 2 3 4 5])
    (matche [a]
            ([ [1 . _] ]
             (== q "first"))
            ([ [_ . o] ]
             (== q ["second" o])))))
;=> ("first" 
;    ["second" (2 3 4 5)])

;; Implicit Variables: https://github.com/frenchy64/Logic-Starter/wiki#matche-sugar-implicit-variables
(run* [q]
  (fresh [a o]
    (== a [1 2 3 4 5])
    (matche [a]
            ([ [1 . o] ]
             (== q ["one" o]))
            ([ [1 2 . ?o] ]
             (== q ["two" ?o]))
            ([ [o . ?o] ]
             (== q ["third" o ?o])))))
;=> (["one" (2 3 4 5)] 
;    ["two" (3 4 5)] 
;    ["third" 1 (2 3 4 5)]