From: Shane McDaniel
Subject: Accessing struct members from a list of structs
Date: 
Message-ID: <c09j71$ih5$1@news-int.gatech.edu>
I have the following code

(defstruct foo x )
 (setq tmpa ( make-foo :x 4 ) )
(setq l '( 'a ) )

and then when I try to do

 ( foo-x (nth 0 l ) )

I get

SYSTEM::%STRUCTURE-REF: 'A is not a structure of type FOO

If the 3rd line above is changed to

(setq l '(a) )

I get a similar error on  'A'

Why doesn't it think it's still a Foo?  How do I access the structure
instance from the list?

Thank you

-shane

From: Artie Gold
Subject: Re: Accessing struct members from a list of structs
Date: 
Message-ID: <c09k5p$14gs9j$1@ID-219787.news.uni-berlin.de>
Shane McDaniel wrote:
> I have the following code
> 
> (defstruct foo x )

OK. You've defined what a `foo' is.

>  (setq tmpa ( make-foo :x 4 ) )

OK. `tmpa' is a `foo'

> (setq l '( 'a ) )

OK. `l' is quoted list containing the symbol `a' (i.e. 'a)
> 
> and then when I try to do
> 
>  ( foo-x (nth 0 l ) )
> 
> I get
> 
> SYSTEM::%STRUCTURE-REF: 'A is not a structure of type FOO

Why does this surprise you? The symbol 'a is not a `foo'. Why would it 
be? It's a *symbol*!

> 
> If the 3rd line above is changed to
> 
> (setq l '(a) )
> 
> I get a similar error on  'A'
> 
> Why doesn't it think it's still a Foo?  How do I access the structure
> instance from the list?

Huh?

Cheers,
--ag
-- 
Artie Gold -- Austin, Texas

"Yeah. It's an urban legend. But it's a *great* urban legend!"
From: Shane McDaniel
Subject: correct problem.
Date: 
Message-ID: <c09lmu$js8$1@news-int.gatech.edu>
OK I'm just plain stupid, lets try this again

(defstruct foo x )
(setq tmpa ( make-foo :x 4 ) )
(setq l '( 'tmpa ) )

now

( foo-x (nth 0 l ) )

gives the error

SYSTEM::%STRUCTURE-REF: TMPA is not a structure of type FOO

same error if I use

(setq l '( tmpa ) )

instead of line 3 above

Basically I want to make a list of foo's and be able to get back at the
member variables.

Thanks

-shane

"Artie Gold" <·········@austin.rr.com> wrote in message
····················@ID-219787.news.uni-berlin.de...
> Shane McDaniel wrote:
> > I have the following code
> >
> > (defstruct foo x )
>
> OK. You've defined what a `foo' is.
>
> >  (setq tmpa ( make-foo :x 4 ) )
>
> OK. `tmpa' is a `foo'
>
> > (setq l '( 'a ) )
>
> OK. `l' is quoted list containing the symbol `a' (i.e. 'a)
> >
> > and then when I try to do
> >
> >  ( foo-x (nth 0 l ) )
> >
> > I get
> >
> > SYSTEM::%STRUCTURE-REF: 'A is not a structure of type FOO
>
> Why does this surprise you? The symbol 'a is not a `foo'. Why would it
> be? It's a *symbol*!
>
> >
> > If the 3rd line above is changed to
> >
> > (setq l '(a) )
> >
> > I get a similar error on  'A'
> >
> > Why doesn't it think it's still a Foo?  How do I access the structure
> > instance from the list?
>
> Huh?
>
> Cheers,
> --ag
> -- 
> Artie Gold -- Austin, Texas
>
> "Yeah. It's an urban legend. But it's a *great* urban legend!"
From: Rahul Jain
Subject: Re: correct problem.
Date: 
Message-ID: <87n07r4kp7.fsf@nyct.net>
"Shane McDaniel" <······················@spamgourmet.com> writes:

> OK I'm just plain stupid, lets try this again
>
> (defstruct foo x )
> (setq tmpa ( make-foo :x 4 ) )
> (setq l '( 'tmpa ) )
>
> now
>
> ( foo-x (nth 0 l ) )
>
> gives the error
>
> SYSTEM::%STRUCTURE-REF: TMPA is not a structure of type FOO

Are you sure it says that and not "'TMPA"?

> same error if I use
>
> (setq l '( tmpa ) )
>
> instead of line 3 above
>
> Basically I want to make a list of foo's and be able to get back at the
> member variables.

(defvar *foos* (list (make-foo :x 1) (make-foo :x 5) (make-foo :x 2)))

-- 
Rahul Jain
·····@nyct.net
Professional Software Developer, Amateur Quantum Mechanicist
From: Shane McDaniel
Subject: Re: correct problem.
Date: 
Message-ID: <c09nka$ks6$1@news-int.gatech.edu>
"Rahul Jain" <·····@nyct.net> wrote in message
···················@nyct.net...
> "Shane McDaniel" <······················@spamgourmet.com> writes:
>
> > OK I'm just plain stupid, lets try this again
> >
> > (defstruct foo x )
> > (setq tmpa ( make-foo :x 4 ) )
> > (setq l '( 'tmpa ) )
> >
> > now
> >
> > ( foo-x (nth 0 l ) )
> >
> > gives the error
> >
> > SYSTEM::%STRUCTURE-REF: TMPA is not a structure of type FOO
>
> Are you sure it says that and not "'TMPA"?

I don't see the difference but that's the error it gives me, copy and pasted

>
> > same error if I use
> >
> > (setq l '( tmpa ) )
> >
> > instead of line 3 above
> >
> > Basically I want to make a list of foo's and be able to get back at the
> > member variables.
>
> (defvar *foos* (list (make-foo :x 1) (make-foo :x 5) (make-foo :x 2)))

I need to 'reference' the foos in a couple places, hence why I was trying to
making variables and then trying to use them in what I thought was a
reference manner.

I'm coming at this from a c++ background, which is probably apparant and
horrible.

-shane
From: Duncan Rose
Subject: Re: correct problem.
Date: 
Message-ID: <5b0a8945.0402100442.2339dce9@posting.google.com>
"Shane McDaniel" <······················@spamgourmet.com> wrote in message news:<············@news-int.gatech.edu>...
> "Rahul Jain" <·····@nyct.net> wrote in message
> ···················@nyct.net...
> > "Shane McDaniel" <······················@spamgourmet.com> writes:
> >
> > > OK I'm just plain stupid, lets try this again
> > >
> > > (defstruct foo x )
> > > (setq tmpa ( make-foo :x 4 ) )
> > > (setq l '( 'tmpa ) )
> > >
> > > now
> > >
> > > ( foo-x (nth 0 l ) )
> > >
> > > gives the error
> > >
> > > SYSTEM::%STRUCTURE-REF: TMPA is not a structure of type FOO
> >
> > Are you sure it says that and not "'TMPA"?
> 
> I don't see the difference but that's the error it gives me, copy and pasted
> 

LispWorks gives me a "(quote tmpa) is not of type FOO" error. Are you sure you
didn't paste the error message from your second attempt "(setq l '(tmpa))"?


"(setq l `(,tmpa))" works for me for what you're attempting, in line 3.

Only being a Lisp newbie myself, I haven't formed particular style rules. But
the above backquote-comma looks a lot worse to me than "(setq l (list tmpa))"
(but it does look more like what you seem to be trying to write).


-Duncan


> >
> > > same error if I use
> > >
> > > (setq l '( tmpa ) )
> > >
> > > instead of line 3 above
> > >
> > > Basically I want to make a list of foo's and be able to get back at the
> > > member variables.
> >
> > (defvar *foos* (list (make-foo :x 1) (make-foo :x 5) (make-foo :x 2)))
> 
> I need to 'reference' the foos in a couple places, hence why I was trying to
> making variables and then trying to use them in what I thought was a
> reference manner.
> 
> I'm coming at this from a c++ background, which is probably apparant and
> horrible.
> 
> -shane
From: Rahul Jain
Subject: Re: correct problem.
Date: 
Message-ID: <87isibojoy.fsf@nyct.net>
··@robotcat.demon.co.uk (Duncan Rose) writes:

> "(setq l `(,tmpa))" works for me for what you're attempting, in line 3.
>
> Only being a Lisp newbie myself, I haven't formed particular style rules. But
> the above backquote-comma looks a lot worse to me than "(setq l (list tmpa))"
> (but it does look more like what you seem to be trying to write).

In that case, what do you think about `'(`'(,tmpa ,,tmpb))? ;)

-- 
Rahul Jain
·····@nyct.net
Professional Software Developer, Amateur Quantum Mechanicist
From: Fred Gilham
Subject: Re: correct problem.
Date: 
Message-ID: <u7bro7rmha.fsf@snapdragon.csl.sri.com>
Shane McDaniel writes:
> I need to 'reference' the foos in a couple places, hence why I was
> trying to making variables and then trying to use them in what I
> thought was a reference manner.
> 
> I'm coming at this from a c++ background, which is probably apparant
> and horrible.

Let's say you do the following:

(defstruct foo x )
(setq tmpa ( make-foo :x 4 ) )
(setq l '( 'tmpa ) )

Do the following in your Lisp session:

* tmpa

#S(FOO :X 4)


So you see that the variable tmpa "evaluates to" the object

   #S(FOO :X 4)

which is the printed representation of your structure.

Note also that

* 'tmpa

TMPA

that is, 'tmpa evaluates to the symbol tmpa.  In other words, when you
put in the quote, you're saying that you don't want the value of the
thing that's quoted, but the thing itself.

So what you really want is simply

(setq l (list tmpa))

or, if you *want* to use a backquote,

(setq l `(,tmpa))


The backquoted version says, "quote the following, except if it or
something contained in it has a comma in front of it, evaluate the
thing preceeded by the comma."

So for example in the following backquoted expressions:

* `tmpa

TMPA

You get the symbol tmpa.

* `,tmpa

#S(FOO :X 4)

You get the value of tmpa.

* `(tmpa)

(TMPA)

You get a list consisting of the symbol tmpa.

* `(,tmpa)

(#S(FOO :X 4))
* 

You get a list consisting of the value of tmpa.



BTW, remember that backquote or "`" and quote or "'" are different.
Quote completely inhibits evaluation in its scope, and it doesn't
allow a comma to force evaluation.  Backquote conditionally inhibits
evaluation --- it's often called "quasiquote" because it is a sort of
softer version of quote --- you can still force evaluation with comma
or comma-at-sign.



Perhaps your confusion arises because you tried something like the
following:

(setf l (tmpa))

or

(setf l ('tmpa))

Here the problem is that when a list is evaluated, the first element
of the list is treated specially --- it must be a symbol with a
function binding (there are exceptions to this but don't worry about
them here).  Thus the list

  (tmpa)

is a function call to the function tmpa (which doesn't exist)

and

  ('tmpa)

is actually

  ((quote tmpa))

which is an illegal function call because (quote tmpa) isn't a valid
function name.

That's why if you want to write a literal list you have to quote it
somehow.

(setf l '(tmpa))

But the above doesn't do what you want because the whole list gets
quoted and you want the *value* of tmpa, not tmpa itself.

I think the simplest way to do what you want is just use the "list"
function

(setf l (list tmpa))

This says, set l to a list whose element is the value of tmpa.

I hope this helps.

-- 
Fred Gilham                                       ······@csl.sri.com
linus doesnt have what they call a GUI which means that you cant use a
mouse and click on things you have to type in words in a different
language probably finnish I dont know but it wont work unless you
speak at least 10 different languages and one of those languages has
to be a slavic language and you have a phd in 10 different languages
and also can do math real real good - From geraldholmes.freeyellow.com
From: Alan Crowe
Subject: Re: correct problem.
Date: 
Message-ID: <86vfmfyzoj.fsf@cawtech.freeserve.co.uk>
Shane McDaniel wrote:

    I'm coming at this from a c++ background, which is
    probably apparant and horrible.

In that case I had better warn you about a hideous gotcha
that lurks around setf and defstruct, waiting to tear ex-C
programmers limb from limb.

This C

struct farm {
  char *wheat;
  char *turnip;
} golden_acre, brookside;
.
.
.
brookside = golden_acre;

cannot really be translated into Lisp.
This apparently successful attempt

(defstruct farm
  wheat
  turnip)

(setf brookside (copy-farm golden-acre))

rather misleads. It is better thought of as a translation
of 

struct farm {
  char *wheat;
  char *turnip;
} *golden_acre, *brookside;

struct farm *copy_farm(struct farm *name)
{
  struct farm *temp;

  temp = (struct farm *)malloc(sizeof (struct farm));
  temp->wheat = name->wheat;
  temp->turnip = name->turnip;
  return temp;
}

brookside = copy_farm(golden_acre);

Now the terrible danger is coming into view.
With pointers to structures

brookside = golden_acre;

makes brookside and golden-acre point to the same
structure. When you harvest brookside's turnips,
golden_acre's turnips mysteriously disappear, because you
have shared structure, and have two names for but a single
farm.

This is what happens when you do

(setf brookside golden-acre)

instead

(setf brookside (copy-farm golden-acre))

C defaults for imperative programming. You are going to be
modifying your structures, so it copies them for you.

Lisp defaults for applicative programming. You are going to
be computing functions of the contents of your structures,
so it shares them to economise on memory. The cannonical
example is

(let ((*print-circle* t))
    (print (maplist #'identity '(a b c))))

((A . #1=(B . #2=(C))) #1# #2#) 

=> ((A B C) (B C) (C))


Both languages are correctly set up with the appropriate
defaults for their own default programming style.

Contrast

(let ((tmpa (make-foo :x 5))
	tmpb)
    (setf tmpb (copy-foo tmpa))
    (setf (foo-x tmpb) "seven")
    (list tmpa tmpb))
=> (#S(FOO :X 5) #S(FOO :X "seven")

with

(let ((tmpa (make-foo :x 5))
	tmpb)
    (setf tmpb tmpa)
    (setf (foo-x tmpb) "seven")
    (list tmpa tmpb))
=> (#S(FOO :X "seven") #S(FOO :X "seven"))

which will afflict you with the bug from hell if no-one has
aroused your suspicions.

Alan Crowe
Edinburgh
Scotland
From: Tim Bradshaw
Subject: Re: correct problem.
Date: 
Message-ID: <fbc0f5d1.0402100749.5cb4dbc1@posting.google.com>
Alan Crowe <····@cawtech.freeserve.co.uk> wrote in message news:<··············@cawtech.freeserve.co.uk>...
>
> Lisp defaults for applicative programming. You are going to
> be computing functions of the contents of your structures,
> so it shares them to economise on memory. The cannonical
> example is
>

I think it's more complicated (and worse) than this.  C and C++ have
this whole object/pointer-to-an-object dichotomy, which forces them to
use these copying assignments.  But they've now dug themselves into an
enormous pit, because copying assignment means, well, copying, and
that means allocating memory and, oh dear, there's no storage
management in C/C++.  But this is not the worst of it: while clawing
your way up the wall of the pit, the wall gives way, and you fall
through into a much larger pit full of tentacled horrors from another
dimension which, slowly, tear you to pieces.  That's because copying
assignment means, well, *copying*, and there's effectively no way for
the language to know what it means to copy an object such that the
copy is another object rather than a pile of mangled flesh lying
twitching on the floor. C++ attempts to repair the wall by letting you
define copy constructors, which are meant to let you get a handle on
this copying process during assignment.  But of course, it fails, and
instead allows the creatures to escape from the pit with consequences
that should not be described in a family newsgroup.

The lesson is: don't confuse assignment with copying, lest you be torn
to bits by things with tentacles and too many mouths.
From: Joe Marshall
Subject: Re: correct problem.
Date: 
Message-ID: <isifhulo.fsf@ccs.neu.edu>
> Shane McDaniel wrote:
>
>     I'm coming at this from a c++ background, which is
>     probably apparant and horrible.
>

Alan Crowe <····@cawtech.freeserve.co.uk> writes:
> In that case I had better warn you about a hideous gotcha
> that lurks around setf and defstruct, waiting to tear ex-C
> programmers limb from limb.
>
> This C
>
> struct farm {
>   char *wheat;
>   char *turnip;
> } golden_acre, brookside;
> .
> .
> .
> brookside = golden_acre;
>
> cannot really be translated into Lisp.

In C, that declaration binds golden_acre and brookside to two
*different* chunks of memory, each able to hold two char*s.

> This apparently successful attempt
>
> (defstruct farm
>   wheat
>   turnip)
>
> (setf brookside (copy-farm golden-acre))
>
> rather misleads. 

In Lisp, a variable can be bound to *any* object and doesn't refer to
one specific chunk of memory.  So something like this:

(setf brookside golden-acre)

Does *not* mean `copy the golden-acre chunk into the brookside chunk',
but rather `make brookside now refer to what golden-acre refers to'.
Two names for the same object.

> When you harvest brookside's turnips, golden_acre's turnips
> mysteriously disappear, because you have shared structure, and have
> two names for but a single farm.

[rest of article snipped]
From: Rahul Jain
Subject: Re: correct problem.
Date: 
Message-ID: <87n07nojwu.fsf@nyct.net>
Alan Crowe <····@cawtech.freeserve.co.uk> writes:

> C defaults for imperative programming. You are going to be
> modifying your structures, so it copies them for you.
>
> Lisp defaults for applicative programming. You are going to
> be computing functions of the contents of your structures,
> so it shares them to economise on memory. The cannonical
> example is

No.

In C, "=" is defined to copy bits.

In Lisp, the copy function of a struct is defined to create a new
structure with the same contents as the old structure. If you want to
define a deeper copier, tell defstruct not to define one itself and
define your own.

-- 
Rahul Jain
·····@nyct.net
Professional Software Developer, Amateur Quantum Mechanicist
From: Frode Vatvedt Fjeld
Subject: Re: correct problem.
Date: 
Message-ID: <2hhdxzv9w9.fsf@vserver.cs.uit.no>
"Shane McDaniel" <······················@spamgourmet.com> writes:

> (defstruct foo x )
> (setq tmpa ( make-foo :x 4 ) )
> (setq l '( 'tmpa ) )

You don't have a problem with defstructs and slots, you have a problem
with understanding the fundamental evaluation rules of lisp. The above
looks like you're throwing in some parens, quotes and symbols for good
measure, not because you have any idea of how it's supposed to
work. You should find some introductory material on lisp to read. When
you understand the fundamentals, the solution to the problem you are
experiencing will be self-evident.

-- 
Frode Vatvedt Fjeld
From: Rmagere
Subject: Re: correct problem.
Date: 
Message-ID: <c0a0u6$ooa$1@news.ox.ac.uk>
Shane McDaniel wrote:
> (setq l '( 'tmpa ) )
>
> same error if I use
>
> (setq l '( tmpa ) )
>
`x is the same as (quote x) which mens return x without evaluating it so
both times you are dealing with the name (or symbol) `tmpa`, not with what
`tmpa` represent; if as you said you want to make a list which includes what
`tmpa` is than try using (setq l (list tmpa)), however you should try to
find some tutorial only and as someone suggested you could have a look at
http:\\www.alu.org, http:\\alu.cliki.net or even better try
http://www.cliki.net/Online%20Tutorial.
From: Alan Crowe
Subject: Re: correct problem.
Date: 
Message-ID: <86y8rbz26r.fsf@cawtech.freeserve.co.uk>
Shane McDaniel sought help with:

    (defstruct foo x )
    (setq tmpa ( make-foo :x 4 ) )
    (setq l '( 'tmpa ) )

    now

    ( foo-x (nth 0 l ) )

    gives the error

    SYSTEM::%STRUCTURE-REF: TMPA is not a structure of type FOO

    same error if I use

    (setq l '( tmpa ) )

    instead of line 3 above

    Basically I want to make a list of foo's and be able to
    get back at the member variables.

Use the power of the Read-Eval-Print-Loop (the REPL to
aficionado's)

Make sure you are set up so that you can recall earlier
expresions, make small changes, and re-run them. In emacs
inferior lisp mode it is control-up-arrow to retrieve the
previous expression. If you forget and just press up-arrow,
you move up your transcript, and then control-up-arrow stops
working.
    "Not at command line"
so you have to press shift-alt-> to get to the command line.

Then you can recall previous expressions, change them a
little, and try again. I'm really laying this point on with
a trowel. 

    LISP IS VERY EASY TO LEARN
        (but only if you take advantage of the REPL)

So,

make-foo makes a structure.
(make-foo :x 4) => #S(FOO :X 4)


list makes a list, of just one item if that is all you give it
(list (make-foo :x 4)) => (#S(FOO :X 4))

You can extract the first item from a list with car
(car (list (make-foo :x 4))) => #S(FOO :X 4)

Then structure accessors work just as you expect
(foo-x (car (list (make-foo :x 4)))) => 4

You can stuff things into variables
(setf tmpa (list (make-foo :x 4))) => (#S(FOO :X 4))
and they return the new value of the variable, which you can
use in your programs, just like in C. More importantly, when
you are using the REPL to learn Lisp, you can see
immediately that something has gone wrong.

Your (setq l '( 'tmpa ) ) => ('TMPA)
which should have immediately set off an alarm bell. The
list should have contained #S(FOO :X 4), but the REPL was
using the abbreviation 'TMPA to tell you that the list
contained (QUOTE TMPA). Try 

(car (setq l '( 'tmpa ) )) => 'TMPA

(car (car (setq l '( 'tmpa ) ))) => QUOTE

Exercise for reader:
(cdr (car (setq l '( 'tmpa ) )))
(car (cdr (car (setq l '( 'tmpa ) ))))
(cadar (setq l '( 'tmpa ) ))

Right, back to variables

(setf tmpa (list (make-foo :x 4))) => (#S(FOO :X 4))
(foo-x (car tmpa)) => 4

Quote lets you type in literal data. So

(foo-x (car '(#S(FOO :X 4)))) => 4

But you almost never want to do this. The reason is that
literal 4 is unrealistic. In a real program you want to fill
slot x with the value of a variable. Contrast

(let ((tmpb 4))
    (foo-x (car (list (make-foo :x tmpb))))) => 4

with
(let ((tmpb 4))
    (foo-x (car '(#s(foo :x tmpb))))) => TMPB

the reader built a foo structure containing the symbol TMPB
in its x slot. QUOTE makes the list data, rather than an
illegal function call. car and foo-x retrieve the symbol.

Whether your learning time is hours or weeks depends on
whether you can recover your childhood and type at the REPL
like it was a new toy :-)

Alan Crowe
Edinburgh
Scotland
From: Rahul Jain
Subject: Re: Accessing struct members from a list of structs
Date: 
Message-ID: <87r7x34lgi.fsf@nyct.net>
Artie Gold <·········@austin.rr.com> writes:

> Shane McDaniel wrote:
>> (setq l '( 'a ) )
>
> OK. `l' is quoted list containing the symbol `a' (i.e. 'a)

Actually, l is bound to a literal constant list containing the list
(QUOTE A), which contains two elements, the symbol QUOTE, and the symbol
A.

What I'm wondering is where on earth the OP is learning this style of
coding. Define your variables, and don't go around making symbols
dynamically bound willy-nilly (assuming that's what the OP's
implementation does). SETQ does _not_ define a variable. It could take
your firstborn son and sacrifice him to Zeus, in some implementations.

-- 
Rahul Jain
·····@nyct.net
Professional Software Developer, Amateur Quantum Mechanicist
From: Shane McDaniel
Subject: Re: Accessing struct members from a list of structs
Date: 
Message-ID: <c09mqe$kba$1@news-int.gatech.edu>
This style is basically me looking at the various online tutorials and
specs.  AI class I'm taking uses lisp and I don't know it, so trying to
learn it on the run. :/  I know I'm probably doing things all sorts of
wrong.

> What I'm wondering is where on earth the OP is learning this style of
> coding. Define your variables, and don't go around making symbols
From: Rahul Jain
Subject: Re: Accessing struct members from a list of structs
Date: 
Message-ID: <87isif4kca.fsf@nyct.net>
"Shane McDaniel" <······················@spamgourmet.com> writes:

> This style is basically me looking at the various online tutorials and
> specs.  AI class I'm taking uses lisp and I don't know it, so trying to
> learn it on the run. :/  I know I'm probably doing things all sorts of
> wrong.

Try starting from www.alu.org and alu.cliki.net.

-- 
Rahul Jain
·····@nyct.net
Professional Software Developer, Amateur Quantum Mechanicist
From: szymon
Subject: Re: Accessing struct members from a list of structs
Date: 
Message-ID: <c0ag5r$m50$1@atlantis.news.tpi.pl>
SHANE WROTE:

> This style is basically me looking at the various online tutorials and
> specs.

Please try this: [http://www-2.cs.cmu.edu/~dst/LispBook/index.html].

This book will teach you (too*) gentle and throughly**.

* be patient ;)
** but it was published in 1990.
I bought "ansi common lisp" by Paul Graham -- i think it's really good (and
up to date).

These books are very easy to read -- I don't know English (and lisp)  -- and 
I can read (and understand) them fast.

regards, szymon.
From: Pascal Bourguignon
Subject: Re: Accessing struct members from a list of structs
Date: 
Message-ID: <87ekt3fnrh.fsf@thalassa.informatimago.com>
Rahul Jain <·····@nyct.net> writes:
> > Shane McDaniel wrote:
> >> (setq l '( 'a ) )
> What I'm wondering is where on earth the OP is learning this style of
> coding. Define your variables, and don't go around making symbols
> dynamically bound willy-nilly (assuming that's what the OP's
> implementation does). SETQ does _not_ define a variable. It could take
> your firstborn son and sacrifice him to Zeus, in some implementations.

Let's assume that the variable has been previously defined with: (defvar l)
and that the OP did not want to bother us with it.

(defvar l)
;; ...
;; lots of interactive stuff
;; ...
(setq l '(a))


-- 
__Pascal_Bourguignon__                     http://www.informatimago.com/
There is no worse tyranny than to force a man to pay for what he doesn't
want merely because you think it would be good for him.--Robert Heinlein
http://www.theadvocates.org/
From: Fred Gilham
Subject: Re: Accessing struct members from a list of structs
Date: 
Message-ID: <u78yjbrmbx.fsf@snapdragon.csl.sri.com>
Pascal Bourguignon <····@thalassa.informatimago.com> writes:

> Rahul Jain <·····@nyct.net> writes:
> > > Shane McDaniel wrote:
> > >> (setq l '( 'a ) )
> > What I'm wondering is where on earth the OP is learning this style of
> > coding. Define your variables, and don't go around making symbols
> > dynamically bound willy-nilly (assuming that's what the OP's
> > implementation does). SETQ does _not_ define a variable. It could take
> > your firstborn son and sacrifice him to Zeus, in some implementations.
> 
> Let's assume that the variable has been previously defined with: (defvar l)
> and that the OP did not want to bother us with it.
> 
> (defvar l)
> ;; ...
> ;; lots of interactive stuff
> ;; ...
> (setq l '(a))
> 

Yes, this is important because if you tell a newbie to use

(defvar l (list tmpa))

and he later does something like

(defvar l (list tmpb))

because he doesn't know any better, he will get really really
confused....

-- 
Fred Gilham                                        ······@csl.sri.com
The rage of dance and style that swept the country in the late '70s
crumbled from serious backlash as disco freaks seemed to finally wake
up and realize what they were wearing.  -- Michael Okwu
From: Joe Marshall
Subject: Re: Accessing struct members from a list of structs
Date: 
Message-ID: <ekt3hua1.fsf@ccs.neu.edu>
"Shane McDaniel" <······················@spamgourmet.com> writes:

> I have the following code
>
> (defstruct foo x )
> (setq tmpa ( make-foo :x 4 ) )

* tmpa
=> #s(foo :x 4)

> (setq l '( 'a ) )

* l
=> ((quote a))

L is now a list with one element.  That element is a list of two
elements, the symbol quote and the symbol A.

* (type-of l)
=> CONS

* (car l)
=> (quote a)

* (cdr l)
=> nil

* (length l)
=> 1

QUOTE means ``don't interpret this as a variable or code, I mean
*literally* this object''.

So if you want a list with your foo object in it, call list.

* (setq l1 (list tmpa))
=> (#s(foo :x 4))

* (nth 0 l)
=> #s(foo :x 4)

* (foo-x (nth 0 l))
=> 4