Structs in arrays

Discussion of Common Lisp

Structs in arrays

Postby Super_Stinger » Wed May 02, 2012 3:58 am

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?
Posts: 5
Joined: Wed May 02, 2012 3:49 am

Re: Structs in arrays

Postby gugamilare » Fri May 04, 2012 11:40 am

Code: Select all
(button-x (aref your-vector position))


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.
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil

Re: Structs in arrays

Postby Super_Stinger » Sun May 06, 2012 11:38 pm

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
Posts: 5
Joined: Wed May 02, 2012 3:49 am

Re: Structs in arrays

Postby MicroVirus » Sun May 27, 2012 3:54 pm

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'
Posts: 11
Joined: Sat Apr 07, 2012 6:20 am

Return to Common Lisp

Who is online

Users browsing this forum: Bing [Bot] and 5 guests