Re: newbie question
In case you are wondering why use symbols instead of string, when dealing with a very limited number of accepted arguments to a function using symbols instead of strings can represent an advantage. If dealing with literal strings (the ones you type like "foo" on the REPL for instance) when they are evaluated a new string instance is created even if the same sequence of characters are used, for instance:
CL-USER> (print-object "sample string" *standard-output*)
#<(SIMPLE-ARRAY CHARACTER (13)) {1003787A0F}>
NIL
CL-USER> (print-object "sample-string" *standard-output*)
#<(SIMPLE-ARRAY CHARACTER (13)) {1003AF664F}>
NIL
This small example shows that for every time you call that form a new object is created for the "sample string", you can notice this by looking at the address for the object in memory and how it is different between the two evaluations {1003787A0F} and {1003AF664F}. Now symbols behave quite differently, when you try to evaluate a symbol (in any given package) the system will check if a symbol instance by that name exists already and will return that instance if it already exists in memory, only creating a new instance if that symbol was never evaluated in the running image (or probably if it has been garbage-collected). Let's repeat the previous example:
CL-USER> (print-object :sample *standard-output*)
#<KEYWORD {10037C36EF}>
NIL
CL-USER> (print-object :sample *standard-output*)
#<KEYWORD {10037C36EF}>
NIL
Now as you can see the memory address is exactly the same meaning that even if you call this function a zillion times only one instance of the object will need to be created, this means that the system won't have to spend so much time allocating memory (and later garbage-collecting it) everytime you call that function.
Just my 2 cents on using symbols as function parameters, sorry to everyone if i'm not entirely correct on all i've written, this is how i see it but i might be somewhat wrong.
Cheers,
Alex
On Sat, Feb 6, 2010 at 4:57 PM, Aleksandar Matijaca
<amatijaca@gmail.com> wrote:
Hi there,
once again, I have some time, and have decided to try learning lisp.
I have been going through the "Practical Common Lisp" book by Peter Seibel,
and I find one of the examples confusing.. I have learned so far that when
specifying a list, one can do it
(:TITLE "home" :ARTIST "dixie chicks" :RIPPED T)
So, to me this says the :title is the name holding a value of "home" (i think I am right about this).
Now, in another example, he has a function save-db
(defun save-db (filename)
(with-open-file (out filename
:direction :output
:if-exists :supersede)
(with-standard-io-syntax
(print *db* out))))
The confusing part to me is that there is NO value beside :direction ?? Why not? If he has
purposefully avoided putting some kind of a parameter, what does that really mean? Does it mean
implied default of "true"? And why is there nothing beside :output ?? If :output is a value of :direction,
shouldn't the above be something like :direction "output" ?? The above seems somehow inconsistent,
I know that with-open-file is macro, and not a lisp native syntax, but still - I would imagine that there
should be some kind of a consistency there...
Thanks, Alex.