Sequence of variables.

Discussion of Common Lisp
Post Reply
black_hat
Posts: 5
Joined: Sun Sep 05, 2010 9:53 am

Sequence of variables.

Post by black_hat » Fri Nov 05, 2010 6:21 pm

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.

FAU

Re: Sequence of variables.

Post by FAU » Fri Nov 05, 2010 7:09 pm

You have to be more precise here I think; do you want those variables to be special or lexical?

Warren Wilkinson
Posts: 117
Joined: Tue Aug 10, 2010 11:24 pm
Location: Calgary, Alberta
Contact:

Re: Sequence of variables.

Post by Warren Wilkinson » Fri Nov 05, 2010 9:16 pm

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:

Code: Select all

(let ((x1 44)) ...)
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))
Need an online wiki database? My Lisp startup http://www.formlis.com combines a wiki with forms and reports.

black_hat
Posts: 5
Joined: Sun Sep 05, 2010 9:53 am

Re: Sequence of variables.

Post by black_hat » Sat Nov 06, 2010 5:59 am

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.

Paul Donnelly
Posts: 148
Joined: Wed Jul 30, 2008 11:26 pm

Re: Sequence of variables.

Post by Paul Donnelly » Sat Nov 06, 2010 4:30 pm

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.

Post Reply