SKILL: Difference Between List and Quote?
From: redacted
Hi,
I’m puzzled: I thought
list
and'
(the single quote operator) were equivalent, but they produce different results in the following examples:
a=1 b=2 c=list(a b)
|- (1 2)
a=1 b=2 c='(a b)
|- (a b)
Can you tell me the difference between these expressions?
From: Damien Diederen <dd@crosstwine.com>
Date: Wed, 23 Jan 2013 09:58:47 +0100
Hi redacted,
Yes. Example 1 is easy; focusing on the right side of the assignment,
list(a b)
is a normal function call. Each argument is to be evaluated, and the
results—the contents of the a
and b
variables—applied to the list
function, which builds a
fresh list of its arguments. The result is thus:
(1 2)
Example 2 is a bit more complicated to understand. The first thing to remember is that:
'(a b)
is just a syntax shortcut for:
quote((a b))
This is very important, because quote
is
not a normal function. It is a special operator which
admits a single argument, and returns it unevaluated. In this
case, the single argument to quote is:
(a b)
which, by the rules of the SKILL/SKILL++ program text parser (the
reader), is a list of two symbols, a
and
b
. The result is thus simply:
(a b)
Note that that list is built once, by the reader, and never
touched again. Every evaluation of quote
will return that
same list object. This is very different from e.g.:
list('a 'b)
which, while it produces a similar-looking result, allocates a fresh list on each call; that difference is crucial if the list if to be modified afterwards!
Hope this helps,
Damien Diederen