Page 1 of 1

Structs in arrays

Posted: Wed May 02, 2012 3:58 am
by Super_Stinger
Hey all! Im still a little new to lisp and trying to grasp its power!

I have created a struct here:

Code: Select all

 (defstruct button X Y W H GRACKON TEXT)
And i have an array that is created to store these structs.

If i (vector-push-extend) a struct into this vector (or array), how would i access X in the button struct stored INSIDE the vector?

Re: Structs in arrays

Posted: Fri May 04, 2012 11:40 am
by gugamilare

Code: Select all

(button-x (aref your-vector position))
or

Code: Select all

(slot-value (aref your-vector position) 'x)
(BTW, Common Lisp is case-insensitive, so 'X' and 'x' refer to the same symbol.

Re: Structs in arrays

Posted: Sun May 06, 2012 11:38 pm
by Super_Stinger
Hey! thanks for your reply, its kinda handy how for lisp everything is upper-case, eliminates silly errors that you would probs find in C >_<.

You helped heaps, cheers

Re: Structs in arrays

Posted: Sun May 27, 2012 3:54 pm
by MicroVirus
This is a late reply, but just wanted to add one remark that you might find handy later on:

Common Lisp is actually case-sensitive at its core. However, when you write a piece of code and then tell the lisp reader to read that piece of code, it upcases all the symbol names it encounters. For instance:

Code: Select all

(eq 'mystring (intern "MYSTRING"))
-> t
(eq 'mystring (intern "mystring"))
-> nil
In the first case, the reader reads 'mystring, interns a symbol named MYSTRING (with all upper-case) into the current package and reads on. When evaluating the intern function adds a symbol with name MYSTRING (again all upper-case) to the current package (but this already exists, so it simply returns the existing symbol) and the two end up being equal according to eq.
In the second case, however, intern actually does intern a new symbol with name mystring (all lowercase) into the current package, which is found to be unequal to the previously defined 'mystring symbol which is all uppercase.

So the point is, Lisp is case-sensitive, but the reader smooths out most case issues for you, but it is good to be aware of when working with certain functions, for instance 'intern'