Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>a typical parameter list for such a function would be:</p> <pre><code>(defun preceders (item vector &amp;key (start 0) (end (length vector)) (test #'eql)) ... ) </code></pre> <p>As you can see it has START and END parameters.</p> <p>TEST is the default comparision function. Use (funcall test item (aref vector i)). Often there is also a KEY parameter...</p> <p>LENGTH is called repeatedly for every recursive call of PRECEDERS.</p> <p>I would do the non-recursive version and move two indexes over the vector: one for the first item and one for the next item. Whenever the next item is EQL to the item you are looking for, then push the first item on to a result list (if it is not member there).</p> <p>For the recursive version, I would write a second function that gets called by PRECEDERS, which takes two index variables starting with 0 and 1, and use that. I would not call POSITION. Usually this function is a local function via LABELS inside PRECEDERS, but to make it a bit easier to write, the helper function can be outside, too.</p> <pre><code>(defun preceders (item vector &amp;key (start 0) (end (length vector)) (test #'eql)) (preceders-aux item vector start end test start (1+ start) nil)) (defun preceders-aux (item vector start end test pos0 pos1 result) (if (&gt;= pos1 end) result ... )) </code></pre> <p>Does that help?</p> <p>Here is the iterative version using LOOP:</p> <pre><code>(defun preceders (item vector &amp;key (start 0) (end (length vector)) (test #'eql)) (let ((result nil)) (loop for i from (1+ start) below end when (funcall test item (aref vector i)) do (pushnew (aref vector (1- i)) result)) (nreverse result))) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload