Created
April 8, 2012 21:08
-
-
Save git2samus/2339850 to your computer and use it in GitHub Desktop.
merge-sort as a list generator
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
(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