Dynamic list values

For the Compleat Fan
Locked
pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Dynamic list values

Post by pjot »

Hi,

I have the following code:

Code: Select all

(set 'a 0)
(set 'b '(0 1 2 3 4 a))
Is there a way to update the 'a' in this list dynamically? Meaning:
b
(0 1 2 3 4 0)
So, during initialization of 'b, the list will have assigned the value of 'a, instead of the symbol 'a itself.

Can this be done without tricks like (push) or (nth-set)? A similar thing is possible with an array, but is it possible with lists also?

Peter

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

You could use expand:

Code: Select all

(set 'L '(1 a 3 4 b 6))
(expand L '((a 2) (b 5))
L => (1 2 3 4 5 6)

; or if 'a' and 'b' are already set previously:

(expand L 'a 'b)
The first method is nice because it treats 'a' and 'b' as local variables similar to 'let'

Lutz

pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post by pjot »

Thanks, exactly what I was looking for!!

Peter

Locked