This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Structs in arrays

4 posts · 2571 views

Hey all! Im still a little new to lisp and trying to grasp its power!

I have created a struct here:
 (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

(button-x (aref your-vector position))
or
(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

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

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:
(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'