Hi all,
I'm having a problem adding a structure to a list.
Is it supposed to work, because I created the structure I can print
it's data but I cannot add that stuct into a list.
Can anyone give me a hint?
setf q1 (make-quadro :pai nil :movPossiveis (qtsFaltam board) :H 0 :G
0 :tabuleiro board))
(print q1) ---->> it prints fine
(cons q1 abertos) ----> doesn't give any error
(print (length abertos)) ------> gives 0 instead of one
Thanks a lot
EtherealSoul
(cons q1 abertos) doesn't alter the value of abertos (or q1), it
returns the result of (cons q1 abertos). You'll need to do a setf, et
al., or have the cons as the statement before exit to get the consed
value.
On Nov 23, 4:54 pm, EtherealSoul <·················@googlemail.com>
wrote:
> Hi all,
>
> I'm having a problem adding a structure to a list.
> Is it supposed to work, because I created the structure I can print
> it's data but I cannot add that stuct into a list.
>
> Can anyone give me a hint?
>
> setf q1 (make-quadro :pai nil :movPossiveis (qtsFaltam board) :H 0 :G
> 0 :tabuleiro board))
> (print q1) ---->> it prints fine
> (cons q1 abertos) ----> doesn't give any error
> (print (length abertos)) ------> gives 0 instead of one
>
> Thanks a lot
> EtherealSoul
EtherealSoul <·················@googlemail.com> writes:
> Hi all,
>
> I'm having a problem adding a structure to a list.
> Is it supposed to work, because I created the structure I can print
> it's data but I cannot add that stuct into a list.
>
> Can anyone give me a hint?
>
> setf q1 (make-quadro :pai nil :movPossiveis (qtsFaltam board) :H 0 :G
> 0 :tabuleiro board))
> (print q1) ---->> it prints fine
> (cons q1 abertos) ----> doesn't give any error
> (print (length abertos)) ------> gives 0 instead of one
There are several things that appear to have you confused here:
thinking that arguments are passed by "reference", that cons is a
destructive operation on its second argument (which is assumed to have
been passed by reference), that the result of cons is the "new" value
of its second argument, <possibly/probably others> ...
None of that is true. cons(1) is a function which takes two values,
creates a new cons(2) cell composed of those two values and returns
this as its value. If you want to change abertos, you have to
explicitly do so. So, to "fix" your current quandry:
(setf abertos (cons q1 abertos))
/Jon
1. http://www.lispworks.com/documentation/HyperSpec/Body/f_cons.htm
2. http://www.lispworks.com/documentation/HyperSpec/Body/t_cons.htm
--
'j' - a n t h o n y at romeo/charley/november com
From: Maciej Katafiasz
Subject: Re: newbie question (using list to save structures )
Date:
Message-ID: <fi7ion$njq$2@news.net.uni-c.dk>
Den Fri, 23 Nov 2007 13:54:39 -0800 skrev EtherealSoul:
> I'm having a problem adding a structure to a list. Is it supposed to
> work, because I created the structure I can print it's data but I
> cannot add that stuct into a list.
>
> Can anyone give me a hint?
I will try, despite my inability to read variable names in your language.
> (cons q1 abertos) ----> doesn't give any error
^^^^^^^^^^^^^^^^^
Here. CONS does not push q1 onto abertos. It creates a new cons cell,
with car set to q1 and cdr set to abertos, and returns it. If you want to
push, use PUSH.
http://www.lisp.org/HyperSpec/Body/fun_cons.html
http://www.lisp.org/HyperSpec/Body/mac_push.html
Cheers,
Maciej
Well thanks a lot for the quick reply to you all.
Now I did understand the confusion I was making thinking that cons
would insert the value, but instead it creates a new list with the
values combined.
Thanks a lot to you all.
Regards
EtherealSoul
On Nov 23, 10:04 pm, Maciej Katafiasz <········@gmail.com> wrote:
> Den Fri, 23 Nov 2007 13:54:39 -0800 skrev EtherealSoul:
>
> > I'm having a problem adding a structure to a list. Is it supposed to
> > work, because I created the structure I can print it's data but I
> > cannot add that stuct into a list.
>
> > Can anyone give me a hint?
>
> I will try, despite my inability to read variable names in your language.
>
> > (cons q1 abertos) ----> doesn't give any error
>
> ^^^^^^^^^^^^^^^^^
> Here. CONS does not push q1 onto abertos. It creates a new cons cell,
> with car set to q1 and cdr set to abertos, and returns it. If you want to
> push, use PUSH.
>
> http://www.lisp.org/HyperSpec/Body/fun_cons.htmlhttp://www.lisp.org/HyperSpec/Body/mac_push.html
>
> Cheers,
> Maciej
From: Maciej Katafiasz
Subject: Re: newbie question (using list to save structures )
Date:
Message-ID: <fi7ko4$njq$3@news.net.uni-c.dk>
Den Fri, 23 Nov 2007 14:14:00 -0800 skrev EtherealSoul:
> Now I did understand the confusion I was making thinking that cons would
> insert the value, but instead it creates a new list with the values
> combined.
Nope, it creates a cons cell. Whilst all lists are (and consist of) cons
cells, not all cons cells represent lists.
Cheers,
Maciej
In article
<····································@d4g2000prg.googlegroups.com>,
EtherealSoul <·················@googlemail.com> wrote:
> Well thanks a lot for the quick reply to you all.
>
> Now I did understand the confusion I was making thinking that cons
> would insert the value, but instead it creates a new list with the
> values combined.
>
PUSH is probably what you want to use.
--
Barry Margolin
Arlington, MA
EtherealSoul <·················@googlemail.com> writes:
> Now I did understand the confusion I was making thinking that cons
> would insert the value, but instead it creates a new list with the
> values combined.
How much work do you think is involved in creating a new
list? Does it consume time and memory proportional to the
length of the list?
CONS is a primitive operation and takes a small fixed amount
of time and memory. You usually use it to build
singly-linked lists, link by link. The data structures you
get are confluent. They share structure.
Changes to one list show up in other lists due to structure sharing.
CL-USER> (defparameter *a* (list 3 4 5))
=> *A*
CL-USER> (defparameter *b* (cons 2 *a*))
=> *B*
CL-USER> *b*
=> (2 3 4 5)
Now *a* and *b* share structure:
CL-USER> (write (list *a* *b*) :circle t)
(#1=(3 4 5) (2 . #1#))
=> ((3 4 5) (2 3 4 5))
CL-USER> (defparameter *c* (cons 'two *a*))
=> *C*
Share as much as you like
CL-USER> (write (list *a* *b* *c*) :circle t)
(#1=(3 4 5) (2 . #1#) (TWO . #1#))
=> ((3 4 5) (2 3 4 5) (TWO 3 4 5))
A change to the shared structure shows up everywhere
CL-USER> (setf (second *a*) 'four)
=> FOUR
CL-USER> (write (list *a* *b* *c*) :circle t)
(#1=(3 FOUR 5) (2 . #1#) (TWO . #1#))
=> ((3 FOUR 5) (2 3 FOUR 5) (TWO 3 FOUR 5))
Is this a null operation?
CL-USER> (setf *c* (copy-list *c*))
=> (TWO 3 FOUR 5)
No, the structure isn't shared any more
CL-USER> (write (list *a* *b* *c*) :circle t)
(#1=(3 FOUR 5) (2 . #1#) (TWO 3 FOUR 5))
=> ((3 FOUR 5) (2 3 FOUR 5) (TWO 3 FOUR 5))
So when we make a change
CL-USER> (setf (second *b*) 'three)
=> THREE
the change to *b* shows up in *a* because they still share,
but *c* is immune
CL-USER> (write (list *a* *b* *c*) :circle t)
(#1=(THREE FOUR 5) (2 . #1#) (TWO 3 FOUR 5))
=> ((THREE FOUR 5) (2 THREE FOUR 5) (TWO 3 FOUR 5))
copy-list is quite important. You may see
(let ((storage (copy-list '(a b c))))
...
(setf (second storage) ...))
or
(let ((storage (list 'a 'b 'c)))
...
(setf (second storage) ...))
You will run into trouble with
(let ((storage '(a b c))) ;wrong
...
(setf (second storage) ...));trouble starts
because (a b c) is shared with the list
(let ((storage '(a b c))) ...)
built by read when the LET form was read in, and you mustn't
change that.
So it matters that CONS only allocates one more link, and
doesn't copy the list like COPY-LIST does. COPY-LIST
consumes time and memory proportional to the length of the
list.
Alan Crowe
Edinburgh
Scotland
In article
<····································@y5g2000hsf.googlegroups.com>,
EtherealSoul <·················@googlemail.com> wrote:
> Hi all,
>
> I'm having a problem adding a structure to a list.
> Is it supposed to work, because I created the structure I can print
> it's data but I cannot add that stuct into a list.
>
> Can anyone give me a hint?
>
> setf q1 (make-quadro :pai nil :movPossiveis (qtsFaltam board) :H 0 :G
> 0 :tabuleiro board))
> (print q1) ---->> it prints fine
> (cons q1 abertos) ----> doesn't give any error
> (print (length abertos)) ------> gives 0 instead of one
>
> Thanks a lot
> EtherealSoul
CONS creates a new cons cell. It does not change any variables.
Compare:
? (let ((l1 (list 1 2 3)))
(cons 'foo l1)
l1)
(1 2 3)
and:
? (let ((l1 (list 1 2 3)))
(setf l1 (cons 'foo l1))
l1)
(FOO 1 2 3)
--
http://lispm.dyndns.org/