Sujet : Re: cloning elements in a list
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lispDate : 23. Jun 2025, 20:56:37
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <103cbhk$1fr6f$1@dont-email.me>
User-Agent : XanaNews/1.18.1.6
Coby Beck wrote:
How to write a lisp function which clones the top-level elements of a list
(first arguement) n ( second argument) times. for eg. (clone 'A B C) 4 )
produces ( AAAABBBBCCCC)
To rewrite your question to what I *think* you mean:
(clone '(A B C) 4 )
=> (A A A A B B B B C C C C)
try this:
CL-USER 143 > (defun stretch (list factor)
(loop for elt in list
nconc (make-list factor :initial-element elt)))
STRETCH
CL-USER 144 > (stretch '(a b c) 4)
(A A A A B B B B C C C C)
Gauche Scheme
(define (stretch the-list n)
(append-map (cut make-list n <>) the-list))
(stretch '(a b c) 4)
===>
(a a a a b b b b c c c c)