Skip to content

Instantly share code, notes, and snippets.

@git2samus
Created April 8, 2012 21:08
Show Gist options
  • Save git2samus/2339850 to your computer and use it in GitHub Desktop.
Save git2samus/2339850 to your computer and use it in GitHub Desktop.
merge-sort as a list generator
(defn merge-step [seq1 seq2]
(let [head1 (first seq1)
head2 (first seq2)]
(cond
(nil? head1) seq2
(nil? head2) seq1
:else (if (< head1 head2)
(cons head1 (merge-step (rest seq1) seq2))
(cons head2 (merge-step seq1 (rest seq2)))))))
(defn divide-step [coll]
(let [coll-length (count coll)]
(if (> coll-length 1)
(let [[seq1 seq2] (split-at (quot coll-length 2) coll)]
(merge-step (divide-step seq1) (divide-step seq2)))
coll)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment