Sujet : Re: Remove the occurences of a given element from a list.
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lispDate : 15. Jul 2024, 05:01:52
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v723et$hf66$1@dont-email.me>
User-Agent : XanaNews/1.18.1.6
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)