(def bsearch (stack needle (o f <) (o offset 0))
(if (is (len stack) 1)
(if (is needle (car stack)) offset nil)
(let (fst lst) (split stack (round (/ (len stack) 2)))
(if (f (last fst) needle)
(bsearch lst needle f (+ offset (len fst)))
(bsearch fst needle f offset)))))
Searches for needle in stack. You can use a custom sort-comparison function as the third argument. offset is used internally to keep where we are in the whole list. Returns nil if there's no such item in the list.(Of course, because Arc uses linked lists, this is actually considerably slower than linear iteration through the list, because every list access is multiplied by O(n). An interesting exercise nonetheless.) |