question about sublists



>From sen1:
 
 How do I transform this to the lisp list, apply your function, and
find the indices where 2 appears?
 
Perhaps this way: code 1-based, observe the change do(( i 1...


 :lisp (defun all-positions-if (items pred)
    (do ((i 1 (1+ i))
         (xs items (cdr xs))
         (acc '() (if (funcall pred (car xs)) (cons i acc) acc)))
        ((endp xs) (nreverse acc))));
 
 
 
 
        
lst:[4, 6, 7, -3, 8, 2, 2, 5, 2];

:lisp (all-positions-if (cdr $lst) (lambda (x) (= x 2)));

result ==> (6 7 9)
  
More generally, this procedure is actually a special case of finding
the inverse of a function.
  
  If (f x) is a lisp function for f:N->N, then the pre-image of k
  
  block([re],re:[], for i:=1 thru 187 do if f(x)=k then endcons(k,re),re);