Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

Of course you have a purpose -- to find a purpose.


comp / comp.lang.lisp / .Re: Replacing elements in a list

SubjectAuthor
o .Re: Replacing elements in a listB. Pym

1
Subject: .Re: Replacing elements in a list
From: B. Pym
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Fri, 7 Jun 2024 11:12 UTC
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: No_spamming@noWhere_7073.org (B. Pym)
Newsgroups: comp.lang.lisp
Subject: .Re: Replacing elements in a list
Date: Fri, 7 Jun 2024 11:12:06 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 40
Message-ID: <v3upu3$21vpk$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Fri, 07 Jun 2024 13:12:07 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="1bc38dad617067a6ea5de53e723fa216";
logging-data="2162484"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19rrsGN6grraSW9pAICp8Rp"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:F1bxLTeY6WMC7GtfRgOczji8/cw=
View all headers

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)

1

rocksolid light 0.9.8
clearnet tor