Monthly Archive:: April 2010

Implementing Binary Search with Clojure

I was trying to implement a simple binary search using a purely functional approach, and after much hacking, googling and wikibooking, came up with this in Clojure.


(defn binarysearch
( [lst n]
(binarysearch lst 0 (dec (count lst)) n))
( [lst lb ub n]
(if (> lb ub) -1 ; this is the case where no element is found
(let [mid (quot (+ lb ub) 2)
mth (nth lst mid)]
(cond
; mid > n, so search lower
(> mth n) (recur lst lb (dec mid) n)
; mid < n, search upper (< mth n) (recur lst (inc mid) ub n) ; else, found, return index (= mth n) mid)))))

Clojure Application with Command Line Arguments

I was recently looking for a method to create an application with Clojure that would allow specification of command line arguments.

I came across an excellent post on Stack Overflow by alanlcode , that provides a spectacular example. I’ve Github’d it for reference.

20 Days of Clojure

Came across an excellent series of blog posts by Lou Franco, where he uses the SICP videos as input to learn more about Clojure.

His explanation of HashMap implementations in Clojure, using multimethods, as well as pointers on parallelizing functional programs are very well written. I’m currently on his day 10 post.

Thinking in C++ by Bruce Eckel is an excellent book

I just finished skimming through Bruce Eckel’s Thinking in C++ book – available for free from his website.

Volume 1 covers the basics pretty well and I didn’t really do much more than glance at it, but volume 2 is highly recommended for its marvelous treatment of the C++ STL containers and algorithms.