Skip to content

Instantly share code, notes, and snippets.

@ehaliewicz
Created May 23, 2012 02:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ehaliewicz/2772843 to your computer and use it in GitHub Desktop.
Save ehaliewicz/2772843 to your computer and use it in GitHub Desktop.
Recursive Binary Search
(defun chop-1 (num tree)
(labels ((recur (num tree start-index)
(if (equalp 0 (length tree)) -1
(let* ((mid-point (truncate (/ (length tree) 2)))
(mid-num (elt tree mid-point)))
(cond
((= (length tree) 0) -1)
((= mid-num num) (+ mid-point start-index))
((= (length tree) 1) -1)
((> mid-num num) (recur num (subseq tree 0 mid-point) 0)) ;; check in first half of tree
(t (recur num (subseq tree mid-point) mid-point)))))))
(recur num tree 0))) ;; check in second half
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment