- Install clojure-mode from Marmalade packages site.
- Define a function to run clojure repl
(defun clojure-repl ()
""
(interactive)
(let* ((java-home (getenv "JAVA_HOME"))
(java-cmd (concat java-home "/bin/java"))
(clojure-home (getenv "CLOJURE_HOME"))
(clojure-version "1.3.0")
(clojure-jar (concat clojure-home "/clojure-" clojure-version ".jar"))
(clojure-cmd (concat java-cmd " -jar " clojure-jar))
)
(run-lisp clojure-cmd)
(set-buffer (get-buffer-create "*clojure*"))
(clojure-mode)
)
)
Learn and use clojure
Wednesday, November 2, 2011
Run clojure repl in inferior-lisp mode of Emacs
It's pretty easy and straightforward to configure Emacs for clojure development.
Thursday, October 27, 2011
shell sort
(defn shell-sort [xs]
(let [l (count xs)
max-h (loop [h 0]
(let [t (+ 1 (* h 3))]
(if (> t l) h (recur t))))]
(loop [xt xs h max-h]
(if (= h 0) xt
(recur (loop [xq xt start 0]
(if (= start h) xq
(recur (loop [xp xq this (+ start h)]
(if (>= this l) xp
(recur (let [tv (xp this)
i (loop [prev (- this h)]
(if (< prev start) start
(if (< (xp prev) tv) (+ prev h)
(recur (- prev h)))))]
(if (= i this) xp
(loop [xo xp curr this prev (- curr h)]
(if (< prev i) (assoc xo i tv)
(recur (assoc xo curr (xo prev))
(- curr h) (- prev h)))))
) (+ this h))))
(+ start 1))))
(/ (- h 1) 3))))
))
insert sort
(defn insert-sort [x]
(let [l (count x)]
(loop [xs x m 1]
(if (>= m l) xs
(let [v (xs m)
i (loop [s m]
(if (= s 0) 0
(if (> v (xs (- s 1))) s (recur (- s 1)))))]
(recur
(loop [x xs s m]
(if (= s i) (assoc x s v)
(recur (assoc x s (x (- s 1))) (- s 1))
))
(+ 1 m)))
))
))
select sort
(defn select-sort
([xs] (select-sort xs 0))
([xs start-idx]
(if (< start-idx (count xs))
(let [start-val (xs start-idx)
min-idx (select-sort xs start-idx start-idx)]
(recur (assoc (assoc xs start-idx
(xs min-idx)) min-idx start-val)
(+ 1 start-idx)))
xs))
([xs start-idx min-idx]
(if (< start-idx (count xs))
(recur xs (+ 1 start-idx)
(if (> (xs min-idx) (xs start-idx))
start-idx min-idx))
min-idx))
)
Tuesday, October 25, 2011
bubble sort
(defn bubble-sort
([xs]
(loop [x xs j (count xs)]
(if (> j 0) (recur (bubble-sort x 0 j) (- j 1))
x)))
([xs i j]
(if (< (+ i 1) j)
(let [x (xs i) y (xs (+ i 1))]
(recur (if (> x y) (assoc (assoc xs (+ i 1) x) i y) xs)
(+ 1 i) j))
xs))
)
Subscribe to:
Comments (Atom)