• .Re: Replacing elements in a list

    From B. Pym@21:1/5 to Kenny Tilton on Fri Jun 7 11:12:06 2024
    Kenny Tilton wrote:

    Hi! I started yesterday programming in lisp and now I've got my first problem... Suppose that I have a list l1 and a list l2: how can I replace the nth element in list l1 with list l2? (The resulting list should be flat) Maybe it should be better modify the structure of l1 than create a brand new list with the features desired...

    Thanks!!! piercarlo

    P.S.: I use GCL 2.2.2

    Two answers:

    (a) (defun nsplice (l1 n l2) ;; this is destructive version
    (when (< -1 n (length l1))
    (let* ((prior (when (plusp n)
    (nthcdr (1- n) l1)))
    (target (or (cdr prior)
    (nthcdr n l1)))
    (remainder (cdr target)))

    (when prior
    (rplacd prior l2))

    (rplacd (last l2) remainder)

    (if prior l1 l2))))

    Scheme:

    (let ((l1 '(0 1 2 3 4 5 6 7 8))
    (l2 '(903 905)))
    `(,@(take l1 2) ,@l2 ,@(drop l1 3)))

    ===>
    (0 1 903 905 3 4 5 6 7 8)

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