Path: ...!weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: "B. Pym" Newsgroups: comp.lang.lisp Subject: Re: doubling list elements at every level using recursion Date: Wed, 17 Jul 2024 15:32:54 -0000 (UTC) Organization: A noiseless patient Spider Lines: 40 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Injection-Date: Wed, 17 Jul 2024 17:32:54 +0200 (CEST) Injection-Info: dont-email.me; posting-host="952af2bce2f08853097b4d4e0d3f356b"; logging-data="2003251"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19h8WenvgNCLOT+wAPt2+Dj" User-Agent: XanaNews/1.18.1.6 Cancel-Lock: sha1:M9VaGfBcp6d0Uny9RT56uRQ2NgA= Bytes: 1912 Pascal J. Bourguignon wrote: > You could slightly generalize double, and accept any atoms. This would > allow you to simplify it. > > You could also use emacs and let it do the indentation for you! > > (defun double (object) > (typecase object > (cons (cons (double (car object)) (double (cdr object)))) > (number (* 2 object)) > (t object))) > > (mapcar (function double) > '( () abc 123 (1 2.0 #C(3 4) a b c (5/2 6 d e f) 7 8 9) )) > --> (NIL ABC 246 (2 4.0 #C(6 8) A B C (5 12 D E F) 14 16 18)) Why not simply (double '(() abc 123 (1 2.0 #C(3 4) a b c (5/2 6 d e f) 7 8 9))) ===> (NIL ABC 246 (2 4.0 #C(6 8) A B C (5 12 D E F) 14 16 18)) Scheme (define (double obj) (cond ((pair? obj) `(,(double (car obj)) ,@(double (cdr obj)))) ((number? obj) (* 2 obj)) (#t obj))) (double 3) ===> 6 (double '(() abc 123 (1 2.0 (3 4) a b c (5/2 6 d e f) 7 8 9))) ===> (() abc 246 (2 4.0 (6 8) a b c (5 12 d e f) 14 16 18))