the exercise says:

"""Define iterative and recursive versions of a function that takes an object x and vector v, and returns a list of all the objects that immediately precede x in v:

> (precedes #\a "abracadabra")

(#\c #\d #\r) """

ok, my solution (iterative mode):

- Code: Select all
`(defun precedes(car vec)`

(let((lista nil))

(dotimes (i (length vec))

(if (and (equal (aref vec i) car)(> i 0))

(setf lista (cons (aref vec (- (position car vec :start i) 1)) lista))))

lista))

online book solution:

- Code: Select all
`(defun presedes (x v)`

(let (acc (v (concatenate 'vector v))) ;; on book ther's v1 instead v...;)

(dotimes (i (length v))

(if (and (eql x (svref v i)) (< 0 i))

(push (svref v (- i 1)) acc)))

(remove-duplicates acc)))

First:

probably my solution runs slower then correct solution, isn't it?? becouse my solution sets 'list' for each iteration and lacks "remove-duplicates " that parse the vector...

then, is my solution more inefficient than correct solution? then is always preferable works with latter?

thanks in advance