Re: How Function Create Variables?



(message (Hello 'daidongLY@xxxxxxxxx)
(you :wrote :on '(Mon, 30 Jul 2007 04:42:22 -0700))
(

d> OH... I get it now...
d> The new variables are created by CONS function,

no, CONS does not create variables. you can create a variable in your code
:)
variable is not a data, it just points to data.

d> (defvar *x* '( (a b) (c d) ))
d> (setf *y* (copy-tree *x*))
d> (setf (first (first *y*)) 'e)

d> Then *x* and *y* are different.
d> *x* => ( (A B) (C D))
d> *y* => ( (E B) (C D))

d> No share atoms between *x* and *y* !

err, after copy-tree conses in *x* and *y* point to same atoms (more
preciously, they point to symbols interned into your current package).
however, *y* have different conses. so you change CAR pointer in the first
cons, so it points to symbol E. certainly, first cons in *x* still points to
symbol A. symbols here are not touched at all.

d> Does this mean ATOM (like 'a or 'e in *x* and *y*) also is copied not
d> just be referenced? I mean that if ATOM is only referenced, *x* and
d> *y* should be the same, because their (first (first ...)) point the
d> same Place in memoery.

ehm, i hope C++ code can clarify this..

struct symbol {};

symbol * A = new symbol;

cons * x = new cons( A, NULL);

cons * y = copy_tree(x);
//in this case it's equivalent to:
cons * y = new cons( x->car, x->cdr);

symbol * E = new symbol;
y->car = E;

now we have x being (A) and y being (E).

you should see here that conses provide indirection. when you change car of
cons, it doesn't change symbol which this car point to, but merely a pointer
itself.
to change symbol, you need additional dereference in C++:

*(y->car) = *E;

it Lisp it's not possible to do this dereference -- you can only change
pointers. some objects are immutable in Lisp -- for example, you cannot
change a number. however, if you have a number in a CONS cell, you can make
that CONS pointing to another number.

d> "Each time a function is called, Lisp creates new bindings to hold the
d> arguments passed by the function's caller"
d> The Book-- 'Practical Lisp'

d> Does this sentence mean that every time a function is called, Lisp
d> will create new variables to hold the value( or pointer? ) of the
d> arguments?

yes, you can think it creates new variables that hold pointers to values.

but it's not always some physical place -- Lisp is very flexible on this. it
can hold those variables in CPU registers in some cases, or it can
completely eliminate variable creation if compiler can prove that
eliminating variable won't change semantics.
for example, in function

(defun foo (x)
(let ((y x))
(print y)))

there are two bindings created -- for x and for y. but compiler can see that
this can be reduced to:

(defun foo (x)
(print x))

moreover, if you have function bar:

(defun bar (z)
(foo z))

in some cases compiler can inline foo function call, making bar effectively:

(defun bar (z)
(print z))

so, as you see, there will be no call to function foo when calling bar, and
no bindings for function foo will be created.

)
(With-best-regards '(Alex Mizrahi) :aka 'killer_storm)
"choose no life")


.



Relevant Pages

  • Re: delete command weirdness
    ... IE if changing the binding of variables is ... If you have *testing* bound to a list, it really points to a cons. ... (defun foo (bar) ... so FOO gets called with the cons. ...
    (comp.lang.lisp)
  • Re: When is Xah going to ease up on Emacs...
    ... and the notion of head also appear in lisp. ... is still a tree or a nested list. ... So, suppose you invented a lisp language, so that there's no cons, but ... So this means, in this language the head, or non-leaf nodes of a tree, ...
    (comp.lang.lisp)
  • Re: question about Clojure immutable structures
    ... for the avoidance of doubt: should a Lisp do this? ... and rich tradition. ... One aspect of this tradition is that CONS builds a ... than lists printing as lists, or the existence of two distinct `false' ...
    (comp.lang.lisp)
  • Re: move confusion with PUSH
    ... Return to top loop level 0. ... Also note the difference between CONS and PUSH. ... and then look at foo, ... Do you have a tutorial for Common Lisp, ...
    (comp.lang.lisp)
  • Re: List diagrams -- Siebel and Touretzky draw them differently
    ... Perhaps someone with such a lisp can comment. ... Next relase ofECLis going to support two word conses ... It also simplifies a lot the code for the garbage collector, ... using two-words for a CONS does two things for you. ...
    (comp.lang.lisp)