Thanks. That does look clearer. Dave On Jul 27, 4:15 pm, Joost <jo...@zeekat.nl> wrote: > > I think the main thing that's confusing here is that you're messing > with offsets and split collections at once. At least is was confusing > to me. :) > > I think for binary search (which implies random lookup is ~ O(n) > anyway) it's clearer if you stick to offsets. > > A few remarks regarding style: > > You don't need letfn; personally I think it's clearer to use either > two separate functions or - in this case - specialize on the > arguments. > > If you define assert_equal as a function, you won't get any good > feedback if the assert fails. Use a macro instead. > > Here's what I made: > > (defn chop > ([val coll] > (chop val coll 0 (count coll))) > ([val coll left right] ; range to test is left ... right - 1, which > makes the code a bit cleaner > (if (< left right) ; are we still in a valid range? > (let [halve (int (/ (- right left) 2)) > index (+ left halve) > found-value (coll index)] > (cond > (= val found-value) index > (> val found-value) (recur val coll (inc index) right) > (< val found-value) (recur val coll left index))) > -1))) ; not found > > (defmacro assert_equal > [v1 v2] > `(assert (= ~v1 ~v2))) > > Note that all of this is pretty low-level but I think it looks kinda > nice and readable.
-- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en