Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Changing Elements in a list with skill

4,024 views
Skip to first unread message

Ralf Geiger

unread,
Dec 12, 2002, 5:43:38 AM12/12/02
to
Hello *,

Suppose I have a list
(1 2 3 4 5 6 7 8 9 10) and I'd like to change the seventh element to a value
e.g. 10. So that the result looks like this: (1 2 3 4 5 6 10 8 9 10)

What is the best way to do this since there is no function like
setnth ...

Best regards
Ralf
--
no signature

Andrew Beckett

unread,
Dec 12, 2002, 9:09:05 AM12/12/02
to
Well, in general operations which require you to change the nth entry in a list
suggest to me that you shouldn't be using a list but an array type structure
(either an array or an association table), because lists are sequentially
accessed from the beginning rather than being random access (or
near-random in the case of association tables). However, provided
the entry you're trying to modify is not too far along the list, you can do:

n=7
a='(1 2 3 4 5 6 7 8 9 10)
rplaca(nthcdr(sub1(n) a) 10)

Note that rplaca is a destructive operation - so it changes the list
in situ.

Regards,

Andrew.

--
Andrew Beckett
Senior Technical Leader
Custom IC Solutions
Cadence Design Systems Ltd

Ralf Geiger

unread,
Dec 12, 2002, 9:26:29 AM12/12/02
to
Andrew Beckett schrieb:
[...]

Thanks Andrew!

Best regards
Ralf

0 new messages