From: Steve Smith
Subject: Symbol pulled from list is of type cons?
Date: 
Message-ID: <87hd8zxiqy.fsf@lucretia.remote.isay.com.au>
Hi,

Can somebody explain the following to me; if I put a symbol in a list
and retrieve it, it returns as type CONS:

    (type-of (car '('sym))) => CONS

Some investigation shows the cons consists of a quote symbol and a
symbols list:

    (caar '('sym)) => QUOTE
    (cdar '('sym)) => (SYM)
    (type-of (cadar '('sym))) => SYMBOL

Can someone explain what this is all about?  And more importantly, how
can I store a symbol in a list in a manner that I recognise it as a
symbol later (eg. through etypecase)?

Cheers,
Steve

From: ········@comail.ru
Subject: Re: Symbol pulled from list is of type cons?
Date: 
Message-ID: <1135403512.265432.255670@z14g2000cwz.googlegroups.com>
Steve Smith wrote:

> Can somebody explain the following to me; if I put a symbol in a list
> and retrieve it, it returns as type CONS:
>
>     (type-of (car '('sym))) => CONS

There is a difference between an object, a form (an object meant
to be evaluated) and an object - a result of evaluation of a
form.

``FOO'' (without double quotes) represents a symbol with name FOO.

``(FOO)'' represents a list with one element - a symbol FOO.

``(QUOTE FOO)'' represents a list with two elements - a symbol
QUOTE and a symbol FOO.

All objects may be evaluated. Either by passing as arguments to
the function EVAL, or by typing into REPL. Objects, passed to the
evaluator, are called forms. There are several rules of
evaluating objects:

The result of evaluating a list, whose first element is symbol
QUOTE, is the second element of the list.

To evaluate a list of form (f a1 ... an), where f is a symbol,
naming a function (notice that the symbol QUOTE does *not* name a
function), objects a1 ... an are to be evaluated, the results of
their evaluation are passed to the function, named f, and its
result is the result of the whole form.

Now, consider object ``(QUOTE ((QUOTE SYM)))''. It is a list of
two elements, the first is the symbol ``QUOTE'', the second - a
list of one element - a list of two symbols ``QUOTE'' and
``SYM''. If you evaluate this form the result will be the second
element of the list - ``((QUOTE SYM))'' - a list of one element -
a list of two symbols.

Now to your question. A list of a symbol ``SYM'' is written as
``(SYM)''. In order to build a form, which evaluates to this
list, you can write a list, which first element is the symbol
``QUOTE'' and the second element - the original list:
``(QUOTE (SYM))'', which can be written shorter as _ '(SYM) _.

CL-USER> (type-of '(sym))
CONS
CL-USER> (car '(sym))
SYM
CL-USER> (type-of (car '(sym)))
SYMBOL
From: Pascal Bourguignon
Subject: Re: Symbol pulled from list is of type cons?
Date: 
Message-ID: <87mzircf4f.fsf@thalassa.informatimago.com>
Steve Smith <·····@internode.on.net> writes:

> Hi,
>
> Can somebody explain the following to me; if I put a symbol in a list
> and retrieve it, it returns as type CONS:
>
>     (type-of (car '('sym))) => CONS
>
> Some investigation shows the cons consists of a quote symbol and a
> symbols list:
>
>     (caar '('sym)) => QUOTE
>     (cdar '('sym)) => (SYM)
>     (type-of (cadar '('sym))) => SYMBOL
>
> Can someone explain what this is all about?  And more importantly, how
> can I store a symbol in a list in a manner that I recognise it as a
> symbol later (eg. through etypecase)?


'x is a shortcut (a reader macro) for (quote x)

'('sym)  is a shortcut for (quote ((quote sym)))

When you evaluate  (quote ((quote sym))), you get the list:   ((quote sym))
which contains one element, the list (quote sym), which contains two symbols.


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
Cats meow out of angst
"Thumbs! If only we had thumbs!
We could break so much!"
From: Steve Smith
Subject: Re: Symbol pulled from list is of type cons?
Date: 
Message-ID: <87acerxg7u.fsf@lucretia.remote.isay.com.au>
"Pascal" == Pascal Bourguignon <····@mouse-potato.com> writes:
> 'x is a shortcut (a reader macro) for (quote x)

> '('sym) is a shortcut for (quote ((quote sym)))

OK, so the solution is to be more explicit:

    (type-of (car (list (quote sym)))) => SYMBOL

or 

    (let ((sym 'sym))
      (type-of (car '(sym)))) => SYMBOL

Is there any other way of defining symbols other than quoting?

Cheers,
Steve
From: Coby Beck
Subject: Re: Symbol pulled from list is of type cons?
Date: 
Message-ID: <ya5rf.6385$km.2102@edtnps89>
"Steve Smith" <·····@internode.on.net> wrote in message 
···················@lucretia.remote.isay.com.au...
>
> Is there any other way of defining symbols other than quoting?

You can explicitly call MAKE-SYMBOL, or probably better for you INTERN, but 
beware of readtable case issues:

CL-USER 18 > (make-symbol "sym")
#:|sym|

CL-USER 19 > (make-symbol "SYM")
#:SYM

CL-USER 20 > (intern "SYM")
SYM
:INTERNAL

CL-USER 21 > (type-of *)
SYMBOL


-- 
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")
From: Pascal Bourguignon
Subject: Re: Symbol pulled from list is of type cons?
Date: 
Message-ID: <87irtfcb4d.fsf@thalassa.informatimago.com>
Steve Smith <·····@internode.on.net> writes:

> "Pascal" == Pascal Bourguignon <····@mouse-potato.com> writes:
>> 'x is a shortcut (a reader macro) for (quote x)
>
>> '('sym) is a shortcut for (quote ((quote sym)))
>
> OK, so the solution is to be more explicit:
>
>     (type-of (car (list (quote sym)))) => SYMBOL
>
> or 
>
>     (let ((sym 'sym))
>       (type-of (car '(sym)))) => SYMBOL
>
> Is there any other way of defining symbols other than quoting?

Symbols are not "defined", they're _interned_, in general, and just
made, occasionnaly.  (see the functions INTERN and MAKE-SYMBOL).

In lisp, like in any other programming languages, there's a
distinction between code and data.  And variables belong to the code.
Values belong to the data.  The only thing is that in lisp there are
bridge between these two aspects, both at run-time and at
compilation-time (not to speak of read-time and macro-expansion time).

So, let's define a variable v, to hold as value  a symbol sym.  Here
are two ways to do it:


(let ((v (intern "SYM")))
   (print v))


(let ((v (quote sym)))
   (print v))


In the first one, at run-time, we "intern" a symbol named "SYM".  This
means that if there is already a symbol named "SYM" visible is the
current package, the function INTERN will just return it, otherwise it
will create a new symbol named "SYM", it will intern it in the current
package, and will return it.  Finally, LET will bind this symbol named
"SYM" to the variable named V.  


In the second expression, we get the same result, but the time when
the symbol is interned is at read time.  When the reader reads this
form, even before compiling or interpreting it, it builds a list of
three elements: LET, ((V (QUOTE SYM))) and (PRINT V), and in so doing
it reads each of these names: "LET", "V", "QUOTE", "SYM", "PRINT" and
"V" (actually it reads the downcase version as I typed them in low
case, but as per the default reader case setting it will upcase them
right away), the it interns all these names to get the symbols LET V
QUOTE SYM PRINT and V again to build the expression.  Since the reader
has already put the symbol SYM in the source expression, we don't need
to _evaluate_ anything to get it, so we use the QUOTE special operator
to avoid further evaluation.  QUOTE returns its argument as-is,
namebly the symbol SYM that the reader put in the list (QUOTE SYM),
and LET binds this symbol SYM to the variable named V.  


Now execute this:
 
(let ((v (quote sym)))
   (print  (list v))        ; 1
   (print  (quote (v)))     ; 2
   (values))

and ponder the output.  Next try:

(print  (quote (sym)))      ; 3
(print  (list sym))         ; 4


In the expression 1,  the value of the variable v, namely the symbol
sym is passed to the function list which put it in a new list and
returns it to be printed.  You get: (SYM)

In the expression 2, the list (V) is left unevaluated by QUOTE and
returned as-is to be printed. You get: (V)

In the expression 3, the list (SYM) is left unevaluated by QUOTE and
returned as-is to be printed. You get: (SYM)

In the expression 4, unless you have defined a special variable named
SYM, you get an error indicating that SYM has no value: SYM is
considered as a variable (program) instead of a value (data).



Now, if you want to "put" a symbol in a list, you need to consider it
as data.  So either you have it as value of a variable, or you must
prevent it to be evaluated using QUOTE:

(let ((v (intern "SYM")))
  (let ((mylist (list v)))
    (print mylist)))

(let ((mylist (list (quote sym))))
  (print mylist))

You can also consider the whole list as data, using QUOTE:

(let ((mylist (quote (sym))))
  (print mylist))

Be carefull that such quoted lists are considered immutable: you
cannot modify them.


(let ((mylist (quote (sym))))
   (setf (car mylist) 'some-other-symbol)) ; INVALID!

(let ((mylist (list (quote sym))))
   (setf (car mylist) 'some-other-symbol)) ; correct!



By now, you should understand what's wrong with:

>     (let ((sym 'sym))
>       (type-of (car '(sym)))) => SYMBOL

In (quote (sym)), sym is not considered as program, as a variable; it
is not evaluated. Therefore the binding of a variable sym to the
symbol sym is useless.  You can write directly:

     (type-of (car '(sym)))


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

There is no worse tyranny than to force a man to pay for what he does not
want merely because you think it would be good for him. -- Robert Heinlein
From: Steve Smith
Subject: Re: Symbol pulled from list is of type cons?
Date: 
Message-ID: <87u0cz6my3.fsf@lucretia.remote.isay.com.au>
"Pascal" == Pascal Bourguignon <····@mouse-potato.com> writes:

<snip good explanation>

> By now, you should understand what's wrong with:

>> (let ((sym 'sym)) (type-of (car '(sym)))) => SYMBOL

Right, I'm using the variable's *symbol*, rather that the symbol
*contained* in the variable:

    (let ((mysym 'sym))
        (car '(mysym))) => MYSYM

(Or more accurately, I'm using the interned symbol that happens to be
bound to the variable by the let form).  It only does what I think it
should by coincidence.

Thanks, I think I have a better handle on quoting now.

Cheers,
Steve
From: Coby Beck
Subject: Re: Symbol pulled from list is of type cons?
Date: 
Message-ID: <gqirf.6444$km.874@edtnps89>
"Steve Smith" <·····@internode.on.net> wrote in message 
···················@lucretia.remote.isay.com.au...
>
> Right, I'm using the variable's *symbol*, rather that the symbol
> *contained* in the variable:
>
>    (let ((mysym 'sym))
>        (car '(mysym))) => MYSYM
>
> (Or more accurately, I'm using the interned symbol that happens to be
> bound to the variable by the let form).  It only does what I think it
> should by coincidence.
>
> Thanks, I think I have a better handle on quoting now.

Just beware of the very common newbie misconception that the ' operator is 
how to make a list[*].  Use LIST instead unless you know what the 
constraints are on using literal data in your programming.

> (let ((mysym 'sym))
    (car '(mysym)))
MYSYM

> (let ((mysym 'sym))
    (car (list mysym)))
SYM

-- 
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")

* This is the fault of all the tutorials and example code out there that 
does just that. 
From: Coby Beck
Subject: Re: Symbol pulled from list is of type cons?
Date: 
Message-ID: <Y65rf.6383$km.1897@edtnps89>
"Steve Smith" <·····@internode.on.net> wrote in message 
···················@lucretia.remote.isay.com.au...
> Hi,
>
> Can somebody explain the following to me; if I put a symbol in a list
> and retrieve it, it returns as type CONS:
>
>    (type-of (car '('sym))) => CONS
>
> Some investigation shows the cons consists of a quote symbol and a
> symbols list:
>
>    (caar '('sym)) => QUOTE
>    (cdar '('sym)) => (SYM)
>    (type-of (cadar '('sym))) => SYMBOL
>
> Can someone explain what this is all about?  And more importantly, how
> can I store a symbol in a list in a manner that I recognise it as a
> symbol later (eg. through etypecase)?
>

It is unnecessary for you to use the second quote, you only want one.

CL-USER 16 > (type-of (car '(sym)))
SYMBOL
CL-USER 17 > (type-of (car (list 'sym)))
SYMBOL

-- 
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")