Re: How Function Create Variables?
- From: "Alex Mizrahi" <udodenko@xxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 30 Jul 2007 14:05:53 +0300
(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")
.
- Follow-Ups:
- Re: How Function Create Variables?
- From: daidongLY@xxxxxxxxx
- Re: How Function Create Variables?
- References:
- How Function Create Variables?
- From: daidongLY@xxxxxxxxx
- How Function Create Variables?
- Prev by Date: How Function Create Variables?
- Next by Date: Re: installing eclipse wm with sbcl 1.0.1
- Previous by thread: How Function Create Variables?
- Next by thread: Re: How Function Create Variables?
- Index(es):
Relevant Pages
|