clojureでバブルソート3
あやぴーさんのご指摘を参考に、バブルソートをいろいろ(主に名前を)いじってみる。
最終的にはremainingのdestructuringを後に遅らせた方が、実際に使うところの近くで定義されるのでわかりやすいんじゃないか、ということに。
(def max-min (juxt max min)) (defn bubble [xs i] (loop [done [] x (first xs) remaining (rest xs) remain-count (count remaining)] (cond (>= i remain-count) (doall (concat done [x] remaining)) :else (let [[y & tail] remaining [bigger smaller] (max-min x y)] (recur (conj done smaller) bigger tail (dec remain-count)))))) (defn bubble-sort [xs] (loop [xs xs, i 0] (let [xs' (bubble xs i)] (if (or (>= i (count xs)) (= xs' xs)) xs (recur xs' (inc i))))))