Skip to main content
added 367 characters in body
Source Link

Common Lisp, 142142 143 bytes

(defun b(a e &optional(l 0)(h(length a)))(if(<= h l)-1(let*((m(ash(+ h l)-1))(x(aref a m)))(if(= x e)m(if(< e x)(b a e l m)(b a e(1+ m)h))))))

In an "unminified" form:Having seen some of the comments about recursive vs iterative, I'd point out that in any implementation with tail-call optimization, the second (original) solution is iterative.

(defun b (a e &optional (l 0) (h (length a)))
  (if (<= h l) -1
      (let* ((m (ash (+ h l) -1))
             (x (aref a m)))
        (if (= x e) m
            (if (< e x)
                (b a e l m)
                (b a e (1+ m) h))))))

pure iterative, with do loop (143):

(lambda(a e)(do((l 0)(h(length a)))((<= h l)-1)(let*((m(ash(+ h l)-1))(x(aref a m)))(if(= x e)(return m)(if(< e x)(setf h m)(setf l(1+ m)))))))
(lambda (a e)
    (do ((l 0)
         (h (length a)))
        ((<= h l) -1)
      (let* ((m (ash (+ h l) -1))
             (x (aref a m)))
        (if (= x e)
            (return m)
            (if (< e x)
                (setf h m)
                (setf l (1+ m)))))))

Examplerecursive (142)

CL-USER> (loop with array = #(1 3 5 6 7 8 8 10)
            for element in '(1 7 5 3 2 4 6)
            collect (b array element))
(0 4 2 1 -1 -1 3)
(defun b(a e &optional(l 0)(h(length a)))(if(<= h l)-1(let*((m(ash(+ h l)-1))(x(aref a m)))(if(= x e)m(if(< e x)(b a e l m)(b a e(1+ m)h))))))
(defun b (a e &optional (l 0) (h (length a)))
  (if (<= h l) -1
      (let* ((m (ash (+ h l) -1))
             (x (aref a m)))
        (if (= x e) m
            (if (< e x)
                (b a e l m)
                (b a e (1+ m) h))))))
CL-USER> (loop with array = #(1 3 5 6 7 8 8 10)
            for element in '(1 7 5 3 2 4 6)
            collect (b array element))
(0 4 2 1 -1 -1 3)

Common Lisp, 142 bytes

(defun b(a e &optional(l 0)(h(length a)))(if(<= h l)-1(let*((m(ash(+ h l)-1))(x(aref a m)))(if(= x e)m(if(< e x)(b a e l m)(b a e(1+ m)h))))))

In an "unminified" form:

(defun b (a e &optional (l 0) (h (length a)))
  (if (<= h l) -1
      (let* ((m (ash (+ h l) -1))
             (x (aref a m)))
        (if (= x e) m
            (if (< e x)
                (b a e l m)
                (b a e (1+ m) h))))))

Example

CL-USER> (loop with array = #(1 3 5 6 7 8 8 10)
            for element in '(1 7 5 3 2 4 6)
            collect (b array element))
(0 4 2 1 -1 -1 3)

Common Lisp, 142 143 bytes

Having seen some of the comments about recursive vs iterative, I'd point out that in any implementation with tail-call optimization, the second (original) solution is iterative.

pure iterative, with do loop (143):

(lambda(a e)(do((l 0)(h(length a)))((<= h l)-1)(let*((m(ash(+ h l)-1))(x(aref a m)))(if(= x e)(return m)(if(< e x)(setf h m)(setf l(1+ m)))))))
(lambda (a e)
    (do ((l 0)
         (h (length a)))
        ((<= h l) -1)
      (let* ((m (ash (+ h l) -1))
             (x (aref a m)))
        (if (= x e)
            (return m)
            (if (< e x)
                (setf h m)
                (setf l (1+ m)))))))

recursive (142)

(defun b(a e &optional(l 0)(h(length a)))(if(<= h l)-1(let*((m(ash(+ h l)-1))(x(aref a m)))(if(= x e)m(if(< e x)(b a e l m)(b a e(1+ m)h))))))
(defun b (a e &optional (l 0) (h (length a)))
  (if (<= h l) -1
      (let* ((m (ash (+ h l) -1))
             (x (aref a m)))
        (if (= x e) m
            (if (< e x)
                (b a e l m)
                (b a e (1+ m) h))))))
CL-USER> (loop with array = #(1 3 5 6 7 8 8 10)
            for element in '(1 7 5 3 2 4 6)
            collect (b array element))
(0 4 2 1 -1 -1 3)
deleted 10 characters in body
Source Link

Common Lisp, 146142 bytes

(defun b(a e &optional(l 0)(h(length a)))(if(<= h l)-1(let*((m(+(ash(-+ h l)-1)l))(x(aref a m)))(if(= x e)m(if(< e x)(b a e l m)(b a e(1+ m)h))))))

In an "unminified" form:

(defun b (a e &optional (l 0) (h (length a)))
  (if (<= h l) -1
      (let* ((m (+ (ash (-+ h l) -1) l))
             (x (aref a m)))
        (if (= x e) m
            (if (< e x)
                (b a e l m)
                (b a e (1+ m) h))))))

Example

CL-USER> (loop with array = #(1 3 5 6 7 8 8 10)
            for element in '(1 7 5 3 2 4 6)
            collect (b array element))
(0 4 2 1 -1 -1 3)

Common Lisp, 146 bytes

(defun b(a e &optional(l 0)(h(length a)))(if(<= h l)-1(let*((m(+(ash(- h l)-1)l))(x(aref a m)))(if(= x e)m(if(< e x)(b a e l m)(b a e(1+ m)h))))))

In an "unminified" form:

(defun b (a e &optional (l 0) (h (length a)))
  (if (<= h l) -1
      (let* ((m (+ (ash (- h l) -1) l))
             (x (aref a m)))
        (if (= x e) m
            (if (< e x)
                (b a e l m)
                (b a e (1+ m) h))))))

Example

CL-USER> (loop with array = #(1 3 5 6 7 8 8 10)
            for element in '(1 7 5 3 2 4 6)
            collect (b array element))
(0 4 2 1 -1 -1 3)

Common Lisp, 142 bytes

(defun b(a e &optional(l 0)(h(length a)))(if(<= h l)-1(let*((m(ash(+ h l)-1))(x(aref a m)))(if(= x e)m(if(< e x)(b a e l m)(b a e(1+ m)h))))))

In an "unminified" form:

(defun b (a e &optional (l 0) (h (length a)))
  (if (<= h l) -1
      (let* ((m (ash (+ h l) -1))
             (x (aref a m)))
        (if (= x e) m
            (if (< e x)
                (b a e l m)
                (b a e (1+ m) h))))))

Example

CL-USER> (loop with array = #(1 3 5 6 7 8 8 10)
            for element in '(1 7 5 3 2 4 6)
            collect (b array element))
(0 4 2 1 -1 -1 3)
Source Link

Common Lisp, 146 bytes

(defun b(a e &optional(l 0)(h(length a)))(if(<= h l)-1(let*((m(+(ash(- h l)-1)l))(x(aref a m)))(if(= x e)m(if(< e x)(b a e l m)(b a e(1+ m)h))))))

In an "unminified" form:

(defun b (a e &optional (l 0) (h (length a)))
  (if (<= h l) -1
      (let* ((m (+ (ash (- h l) -1) l))
             (x (aref a m)))
        (if (= x e) m
            (if (< e x)
                (b a e l m)
                (b a e (1+ m) h))))))

Example

CL-USER> (loop with array = #(1 3 5 6 7 8 8 10)
            for element in '(1 7 5 3 2 4 6)
            collect (b array element))
(0 4 2 1 -1 -1 3)