Page 1 of 1
Sequence of variables.
Posted: Fri Nov 05, 2010 6:21 pm
by black_hat
I am trying to create a sequence of variables say x1,x2,...,xn and I would greatly appreciate some feedback. I would like to think the code would be as simple as:
Code: Select all
(dotimes (i 10 nil)
(setf xi 'value for xi))
so that in the end I have
Code: Select all
>x1
--> value for x1
>x2
--> value for x2
...
Thanks in advance for any comments.
Re: Sequence of variables.
Posted: Fri Nov 05, 2010 7:09 pm
by FAU
You have to be more precise here I think; do you want those variables to be special or lexical?
Re: Sequence of variables.
Posted: Fri Nov 05, 2010 9:16 pm
by Warren Wilkinson
When I read this problem I thought I was having Deja-vu
A question a lot like this was asked only a few weeks ago:
viewtopic.php?f=2&t=854
In anycase, having a variable just means you have a direct handle on something, for example:
Is basically going to stick the value 44 into one of your CPU registers which you then refer to as x1 for future manipulation.
When you have a lot of data it isn't possible to put it all into registers, and having a different variable for each value will result in brain overload. The solution is indirection. Instead of having x1, x2, x3... xN, you could just have a list and an offset into that list.
Code: Select all
(let ((x (list 0 1 2 3 4 5 6 7 8)))
(format t "~%X@4: ~d" (elt x 4))
(setf (elt x 6) 'a-new-value)
(format t "~%X is now: ~a" x))
Re: Sequence of variables.
Posted: Sat Nov 06, 2010 5:59 am
by black_hat
Thanks Warren and sorry for not recognizing your other post. After rethinking "how" I was going to use the data in the variables at a later point I decided to simply go with a list containing the data of the n variables. Later when I add to the list (the list will grow), since the operations will be in succession, I will just append to the end of the list thus I can access with the nth function.
Re: Sequence of variables.
Posted: Sat Nov 06, 2010 4:30 pm
by Paul Donnelly
black_hat wrote:Thanks Warren and sorry for not recognizing your other post. After rethinking "how" I was going to use the data in the variables at a later point I decided to simply go with a list containing the data of the n variables. Later when I add to the list (the list will grow), since the operations will be in succession, I will just append to the end of the list thus I can access with the nth function.
It sounds like you want an array. A list will work, but IMO it's never too early to start thinking about choosing the right data structure. Trying to fake arrays with variables is a common newbie mistake. Note that I'm assuming you'll actually be accessing elements by index (e.g. a list with nth, or an array with aref). For other algorithms, a list might be the right choice.
EDIT: By the way, the reason your attempt with setf didn't work is because setf's first argument doesn't get evaluated. It's important to be aware of whether you're calling a function (all arguments will be evaluated) or a macro/special form (argument evaluation depends on the form's definition). Setf's first argument is a
place, which is a bit of data that setf examines to decide what specifically to do.