Last active
August 29, 2015 14:05
-
-
Save minimal/ae65c0026460a5fdcba5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns corematch-exp.core | |
(:require [clojure.core.match :refer (match)])) | |
(defn foo | |
"I don't do a whole lot." | |
[x] | |
(println x "Hello, World!")) | |
(let [x [1 2 3]] | |
(match [x] | |
[[_ _ 2]] :a0 | |
[[1 1 3]] :a1 | |
[[1 2 3]] :a2 | |
:else :a3)) | |
(defn gcd [n m] | |
(match [n m] | |
[n 0] n | |
[0 m] m | |
[n m] (if (> n m) | |
(recur (- n m) m) | |
(recur n (- m n))))) | |
(defn gcd2 [n m] | |
(match [[n m]] | |
[[a 0]] a | |
[[0 b]] b | |
[[a b] :guard [(fn [[x y]] (> x y))]] (recur (- a b) b) | |
[[a b]] (recur a (- b a)))) | |
(defn gcd3 [n m] | |
(match [[n m]] | |
[[a 0]] a | |
[[0 b]] b | |
[[a b] :guard (partial apply >)] (recur (- a b) b) | |
[[a b]] (recur a (- b a)))) | |
;; most like haskell: | |
(defn gcd4 [n m] | |
(match [n m] | |
[n 0] n | |
[0 m] m | |
[n m :guard (fn [_] (> n m))] (recur (- n m) m) | |
[n m] (recur n (- m n)))) | |
;; more efficient, no implicit lets: | |
(defn gcd5 [n m] | |
(match [n m] | |
[_ 0] n | |
[0 _] m | |
[_ _ :guard (fn [_] (> n m))] (recur (- n m) m) | |
:else (recur n (- m n)))) | |
(comment | |
;; gcd :: Number -> Number -> Number | |
;; gcd n 0 = n | |
;; gcd 0 n = n | |
;; gcd n m | n > m = gcd (n - m) m | |
;; gcd n m = gcd n (m - n) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment