Re: How Function Create Variables?



(message (Hello 'daidongLY@xxxxxxxxx)
(you :wrote :on '(Mon, 30 Jul 2007 03:17:37 -0700))
(

d> The Common-User-Package's Function: copy-seq is not deep enough to
d> copy old list to a new list as it's more fit for sequence, So here is
d> a new deep-copy function:

COPY-TREE does deep copy.

d> (defun my-copylist (list)
d> (if (or (not list) (not (listp list)))
d> list
d> (cons (my-copylist (first list))
d> (my-copylist (rest list))))

d> This works well even for the lists like ( (a b) (c d) ), However I
d> don't know how can this function create these new cons cells.

each time you call CONS it creates a new cell.

d> outside variables. But in 'my-copylist' function, it seems that the
d> variable -- 'list' is bound from the parameter -- 'list'. Then how
d> could this new list be created?

if thing is CONS, it gets copied -- you create a new CONS calling CONS
function.
if thing is ATOM (not-a-cons), old value is referenced.

d> I don't know whether i have clearly stated my question, so i wanted to
d> ask it again: What's the difference in local/dynamic variables
d> creating/binding inside a function? Between CL and the C-style
d> programming language...

still i do not clearly understand your question, but i suspect that you get
confusion because C often passes variables by value, while Lisp passes by
reference (that's not always strictly true, but it's closest analogy).

suppose cons defined in such way:

struct cons : object { object * car; object * cdr;

//a constructor that initializes fields
cons( object * car, object* cdr)
: car(car), cdr(cdr)
{}

}

copy_tree can have following prototype in C:

cons * copy_tree (object * thing);

as you see, it receives and returns pointer. but we do not need to write *
in Lisp because everything is passed as a pointer.
then we probe if thing is a cons (i'm using C++ syntax here):

cons* p_cons = dynamic_cast<cons*> (thing);

//and if it's a cons we'll copy it:
if (p_cons) {
return new cons (
copy_tree(p_cons->car),
copy_tree(p_cons->cdr)
);
} else return thing; //otherwise return thing as is.

in a low level war new_cons can be implemented as a function in C:


cons* new_cons( object * car, object * cdr) {
cons * pc = (cons*) malloc (sizeof(cons));
pc->car = car;
pc->cdr = cdr;
return pc;
}

in Lisp semantics is pretty same, but it doesn't need to specially denote
pointers as everything is a pointer, indirections are done automatically,
and memory management is done automatically.

as for variables, i hope at this point you see that they are not much
important and they do not participate in creation of anything -- they are
just pointers, you can have many pointers pointing to single value, or just
pointing to nothing.

)
(With-best-regards '(Alex Mizrahi) :aka 'killer_storm)
"scorn")


.



Relevant Pages

  • Re: Pass by value
    ... I'd have to pass by reference or by pointer. ... there are function that can change them, for example, for CONS ... most of functions on lists come in destructive ... so it might be easier to prefer non-destructive functions and thus avoid ...
    (comp.lang.lisp)
  • Re: whats the meaning of "car","cdr","cons" in trees algorithm?
    ... To summarize (since I don't know exactly how the tree is created this is my ... cons is a struct that contains two pointer, ...
    (microsoft.public.vc.language)
  • Re: whats the meaning of "car","cdr","cons" in trees algorithm?
    ... a flashback from the days when I programmed in Lisp! ... > To summarize (since I don't know exactly how the tree is created this is ... cons is a struct that contains two pointer, ...
    (microsoft.public.vc.language)
  • Re: whats the meaning of "car","cdr","cons" in trees algorithm?
    ... cons is a struct that contains two pointer, ... CAR = Contends of the Address Register ... CDR = Contends of the Data Register ...
    (microsoft.public.vc.language)
  • Re: How Function Create Variables?
    ... if thing is CONS, it gets copied -- you create a new CONS calling CONS ... just pointers, you can have many pointers pointing to single value, or just ... The Book-- 'Practical Lisp' ...
    (comp.lang.lisp)