Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

A light wife doth make a heavy husband. -- Wm. Shakespeare, "The Merchant of Venice"


comp / comp.lang.lisp / Re: Remove the occurences of a given element from a list.

SubjectAuthor
o Re: Remove the occurences of a given element from a list.B. Pym

1
Subject: Re: Remove the occurences of a given element from a list.
From: B. Pym
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Mon, 15 Jul 2024 03:01 UTC
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Nobody447095@here-nor-there.org (B. Pym)
Newsgroups: comp.lang.lisp
Subject: Re: Remove the occurences of a given element from a list.
Date: Mon, 15 Jul 2024 03:01:52 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 38
Message-ID: <v723et$hf66$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Mon, 15 Jul 2024 05:01:53 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="62b7164feba54420b66fafa198f511b0";
logging-data="572614"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+gFhew2TZziY69sNIY70kL"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:V30/TkBY2BdIUK1FiRlVmfl1LAk=
View all headers

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)

1

rocksolid light 0.9.8
clearnet tor