;; 4.1 Arrays (deftest test-vector() (check (equal "hello" (svref (vector "hello" 'N 9) 0)))) ;;; 4.2 Binary search ;; binary search function ;; (defun bin-search (obj vec) (let ((len (length vec))) (and (not (zerop len)) (finder obj vec 0 ( - len 1))))) (defun finder (obj vec start end) ;(format t "~A% ; " (subseq vec start (+ end 1))) (let ((range (- end start))) (if (zerop range) (if (eql obj (aref vec start)) obj nil) (let ((mid (+ start (round (/ range 2))))) (let ((obj2 (aref vec mid))) (if (< obj obj2) (finder obj vec start (- mid 1)) (if (> obj obj2) (finder obj vec (+ mid 1) end) obj))))))) (deftest test-bin-search() (check (eql 3 (bin-search 3 #(0 1 2 3 4 5 6 7 8))) (eql nil (bin-search 9 #(0 1 2 3 4 5 6 7 8)))))