guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Weird behavior of hash-table


From: John Cowan
Subject: Re: Weird behavior of hash-table
Date: Sun, 24 Nov 2019 13:15:29 -0500

On Sun, Nov 24, 2019 at 3:58 AM <address@hidden> wrote:

So the best thing for one's brain is to train it to read #(...)
> as some weird relative of '(...)?
>

Not really: that oversimplification will get you into trouble in more
complex cases.

In Scheme (and Lisp languages generally), there are two contexts for
interpreting S-expressions: datum context and expression context.  In datum
context, as when you are calling "read", the S-expression (foo bar baz) is
a list of three elements.  In expression context, it is either syntax (if
foo is a macro) or a call on the procedure named foo, passing it the values
of the variables bar and baz.

The purpose of quote is to allow you to embed a datum into expression
context.  3 has the same value as a datum and as an expression, so if you
want an expression that evaluates to 3, either 3 or (quote 3), which is
abbreviated '3, will work.  In datum context, though, 3 is a number and '3
is a two-element list whose first element is the symbol named "quote" and
whose second element is 3.  They *evaluate* to the same thing, but don't
*represent* the same datum.

But symbols and lists don't work this way: in expression context, foo's
value is whatever the variable named foo is bound to, but the value of 'foo
is the symbol foo.  Just like 3, in datum context foo and 'foo are *never*
the same thing: the first is a symbol, the second is a two-element list
whose members are the symbols named "quote" and "foo". The same is true for
(foo bar baz) and '(foo bar baz): they don't mean the same thing either in
expression context or in datum context.

Now vector syntax is like number syntax: it has the same value as an
expression or a datum.  So #(a b c) and '#(a b c) have the same value in
expression context, but mean different things in datum context.  What is
more, once in datum context, always in datum context: you don't write '('a
'b 'c) unless you actually want a list of three two-element lists.  So
either #('a 'b 'c) or (in expression context) '#('a 'b 'c) is a vector of
two-element lists, which is how the OP got into trouble.

Is there a corresponding weird relative of `(...)?


No, because the elements of a vector literal, like those of a quoted list,
are in datum context.  The way to achieve that is `(vector 1 ,bar 2 ,baz).
This works because (vector ...) is a procedure call and its elements are in
expression context.



John Cowan          http://vrici.lojban.org/~cowan        address@hidden
So they play that [tune] on their fascist banjos, eh?
        --Great-Souled Sam


reply via email to

[Prev in Thread] Current Thread [Next in Thread]