1
0
Fork 0
cl-sites/guile.html_node/Symbols.html
2024-12-17 12:49:28 +01:00

140 lines
6.3 KiB
HTML

<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.1, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- This manual documents Guile version 3.0.10.
Copyright (C) 1996-1997, 2000-2005, 2009-2023 Free Software Foundation,
Inc.
Copyright (C) 2021 Maxime Devos
Copyright (C) 2024 Tomas Volf
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with no
Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A
copy of the license is included in the section entitled "GNU Free
Documentation License." -->
<title>Symbols (Guile Reference Manual)</title>
<meta name="description" content="Symbols (Guile Reference Manual)">
<meta name="keywords" content="Symbols (Guile Reference Manual)">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content=".texi2any-real">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link href="index.html" rel="start" title="Top">
<link href="Concept-Index.html" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Data-Types.html" rel="up" title="Data Types">
<link href="Keywords.html" rel="next" title="Keywords">
<link href="Strings.html" rel="prev" title="Strings">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
div.example {margin-left: 3.2em}
span:hover a.copiable-link {visibility: visible}
-->
</style>
<link rel="stylesheet" type="text/css" href="https://www.gnu.org/software/gnulib/manual.css">
</head>
<body lang="en">
<div class="subsection-level-extent" id="Symbols">
<div class="nav-panel">
<p>
Next: <a href="Keywords.html" accesskey="n" rel="next">Keywords</a>, Previous: <a href="Strings.html" accesskey="p" rel="prev">Strings</a>, Up: <a href="Data-Types.html" accesskey="u" rel="up">Data Types</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<h4 class="subsection" id="Symbols-1"><span>6.6.6 Symbols<a class="copiable-link" href="#Symbols-1"> &para;</a></span></h4>
<a class="index-entry-id" id="index-Symbols"></a>
<p>Symbols in Scheme are widely used in three ways: as items of discrete
data, as lookup keys for alists and hash tables, and to denote variable
references.
</p>
<p>A <em class="dfn">symbol</em> is similar to a string in that it is defined by a
sequence of characters. The sequence of characters is known as the
symbol&rsquo;s <em class="dfn">name</em>. In the usual case &mdash; that is, where the symbol&rsquo;s
name doesn&rsquo;t include any characters that could be confused with other
elements of Scheme syntax &mdash; a symbol is written in a Scheme program by
writing the sequence of characters that make up the name, <em class="emph">without</em>
any quotation marks or other special syntax. For example, the symbol
whose name is &ldquo;multiply-by-2&rdquo; is written, simply:
</p>
<div class="example lisp">
<pre class="lisp-preformatted">multiply-by-2
</pre></div>
<p>Notice how this differs from a <em class="emph">string</em> with contents
&ldquo;multiply-by-2&rdquo;, which is written with double quotation marks, like
this:
</p>
<div class="example lisp">
<pre class="lisp-preformatted">&quot;multiply-by-2&quot;
</pre></div>
<p>Looking beyond how they are written, symbols are different from strings
in two important respects.
</p>
<p>The first important difference is uniqueness. If the same-looking
string is read twice from two different places in a program, the result
is two <em class="emph">different</em> string objects whose contents just happen to be
the same. If, on the other hand, the same-looking symbol is read twice
from two different places in a program, the result is the <em class="emph">same</em>
symbol object both times.
</p>
<p>Given two read symbols, you can use <code class="code">eq?</code> to test whether they are
the same (that is, have the same name). <code class="code">eq?</code> is the most
efficient comparison operator in Scheme, and comparing two symbols like
this is as fast as comparing, for example, two numbers. Given two
strings, on the other hand, you must use <code class="code">equal?</code> or
<code class="code">string=?</code>, which are much slower comparison operators, to
determine whether the strings have the same contents.
</p>
<div class="example lisp">
<pre class="lisp-preformatted">(define sym1 (quote hello))
(define sym2 (quote hello))
(eq? sym1 sym2) &rArr; #t
(define str1 &quot;hello&quot;)
(define str2 &quot;hello&quot;)
(eq? str1 str2) &rArr; #f
(equal? str1 str2) &rArr; #t
</pre></div>
<p>The second important difference is that symbols, unlike strings, are not
self-evaluating. This is why we need the <code class="code">(quote &hellip;)</code>s in the
example above: <code class="code">(quote hello)</code> evaluates to the symbol named
&quot;hello&quot; itself, whereas an unquoted <code class="code">hello</code> is <em class="emph">read</em> as the
symbol named &quot;hello&quot; and evaluated as a variable reference &hellip; about
which more below (see <a class="pxref" href="Symbol-Variables.html">Symbols as Denoting Variables</a>).
</p>
<ul class="mini-toc">
<li><a href="Symbol-Data.html" accesskey="1">Symbols as Discrete Data</a></li>
<li><a href="Symbol-Keys.html" accesskey="2">Symbols as Lookup Keys</a></li>
<li><a href="Symbol-Variables.html" accesskey="3">Symbols as Denoting Variables</a></li>
<li><a href="Symbol-Primitives.html" accesskey="4">Operations Related to Symbols</a></li>
<li><a href="Symbol-Read-Syntax.html" accesskey="5">Extended Read Syntax for Symbols</a></li>
<li><a href="Symbol-Uninterned.html" accesskey="6">Uninterned Symbols</a></li>
</ul>
</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="Keywords.html">Keywords</a>, Previous: <a href="Strings.html">Strings</a>, Up: <a href="Data-Types.html">Data Types</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>