Last active
December 12, 2015 04:08
-
-
Save arrdem/4712032 to your computer and use it in GitHub Desktop.
A simple msort implementation in Clojure
This file contains hidden or 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
(defmacro recurse [v] | |
(if (= v :left) | |
`(recur ~'l (rest ~'r) (conj ~'res (first ~'r))) | |
`(recur (rest ~'l) ~'r (conj ~'res (first ~'l))))) | |
(defn %merge [l r] | |
(loop [l l | |
r r | |
res []] | |
(if (every? empty? [l r]) | |
res | |
(cond | |
(empty? l) (recurse :left) | |
(empty? r) (recurse :right) | |
(>= (first l) (first r)) (recurse :left) | |
(< (first l) (first r)) (recurse :right))))) | |
(defn msort [seq] | |
(let [count (/ (count seq) 2) | |
left (take count seq) | |
right (drop count seq)] | |
(println left right) | |
(if (empty? right) | |
left | |
(%merge (msort left) (msort right))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment