• Re: cloning elements in a list

    From B. Pym@21:1/5 to All on Mon Sep 2 03:39:39 2024
    How to write a lisp function which clones the top-level elements of a list
    L
    (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 lst factor)
    (append-map
    (cut make-list factor <>)
    lst))

    (stretch '(a b c) 3)
    ===>
    (a a a b b b c c c)

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