• Re: Remove the occurences of a given element from a list.

    From B. Pym@21:1/5 to Pascal Bourguignon on Mon Jul 15 03:01:52 2024
    Pascal Bourguignon wrote:

    Here is for example, a function that is both iterative and
    recursive. but not stupid (even if it could be written better with
    LOOP):

    (defun find-depths (target list)
    "Returns a list of depths where occurences of TARGET are found in LIST"
    (let ((results '()))
    (dolist (item list (nreverse (mapcar (function 1+) results)))
    (cond
    ((eql target item)
    (push 0 results))
    ((listp item)
    (setf results (nreconc (find-depths target item) results)))))))

    C/USER[139]> (find-depths 'a '(a a ((a b (c a d))) a))
    (1 1 3 4 1)
    C/USER[140]> (find-depths 'a '(a (((a)))))
    (1 4)

    Gauche Scheme

    (define (find-depths target lst :optional (depth 1))
    (append-map
    (lambda (item)
    (cond ((eqv? item target) (list depth))
    ((list? item) (find-depths target item (+ depth 1)))
    (#t '())))
    lst))

    (find-depths 'a '(a a ((a b (c a d))) a))
    ===>
    (1 1 3 4 1)

    (find-depths 'a '(a (((a)))))
    ===>
    (1 4)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)