Permalink
Browse files

Gnomesort: slightly more idiomatic version

  • Loading branch information...
1 parent 729aa56 commit 1025dd8d92e8c7b9fb7156d9c26b5632bf8bf0c8 @purcell committed Jul 5, 2011
Showing with 10 additions and 11 deletions.
  1. +10 −11 gnomesort.clj
View
@@ -4,23 +4,22 @@
(defn gnomesort
([c] (gnomesort c <))
([c pred]
- (if (empty? c)
- c
- (loop [x [] y c]
- (cond (empty? y) x
- (empty? x) (recur (take 1 y) (rest y))
- true (let [y1 (first y) ys (rest y) zx (last x)]
- (if (pred y1 zx)
- (recur (butlast x) (concat (list y1 zx) ys))
- (recur (concat x (list y1)) ys))))))))
+ (loop [x [] [y1 & ys :as y] (seq c)]
+ (cond (empty? y) x
+ (empty? x) (recur (list y1) ys)
+ true (let [zx (last x)]
+ (if (pred y1 zx)
+ (recur (butlast x) (concat (list y1 zx) ys))
+ (recur (concat x (list y1)) ys)))))))
+
(comment "
gnomeSort [] = []
-gnomeSort xs = gs [] xs
+gnomeSort xs = gs [] xs
where
gs xs [] = xs
gs [] (y:ys) = gs [y] ys
gs xs (y:ys) | y < zx = gs (init xs) (y:zx:ys)
- | otherwise = gs (xs++[y]) ys
+ | otherwise = gs (xs++[y]) ys
where zx = last xs")

0 comments on commit 1025dd8

Please sign in to comment.