134 lines
6 KiB
HTML
134 lines
6 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>Symbol Keys (Guile Reference Manual)</title>
|
||
|
|
||
|
<meta name="description" content="Symbol Keys (Guile Reference Manual)">
|
||
|
<meta name="keywords" content="Symbol Keys (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="Symbols.html" rel="up" title="Symbols">
|
||
|
<link href="Symbol-Variables.html" rel="next" title="Symbol Variables">
|
||
|
<link href="Symbol-Data.html" rel="prev" title="Symbol Data">
|
||
|
<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}
|
||
|
strong.def-name {font-family: monospace; font-weight: bold; font-size: larger}
|
||
|
-->
|
||
|
</style>
|
||
|
<link rel="stylesheet" type="text/css" href="https://www.gnu.org/software/gnulib/manual.css">
|
||
|
|
||
|
|
||
|
</head>
|
||
|
|
||
|
<body lang="en">
|
||
|
<div class="subsubsection-level-extent" id="Symbol-Keys">
|
||
|
<div class="nav-panel">
|
||
|
<p>
|
||
|
Next: <a href="Symbol-Variables.html" accesskey="n" rel="next">Symbols as Denoting Variables</a>, Previous: <a href="Symbol-Data.html" accesskey="p" rel="prev">Symbols as Discrete Data</a>, Up: <a href="Symbols.html" accesskey="u" rel="up">Symbols</a> [<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="subsubsection" id="Symbols-as-Lookup-Keys"><span>6.6.6.2 Symbols as Lookup Keys<a class="copiable-link" href="#Symbols-as-Lookup-Keys"> ¶</a></span></h4>
|
||
|
|
||
|
<p>Given their efficiency and descriptive power, it is natural to use
|
||
|
symbols as the keys in an association list or hash table.
|
||
|
</p>
|
||
|
<p>To illustrate this, consider a more structured representation of the car
|
||
|
properties example from the preceding subsection. Rather than
|
||
|
mixing all the properties up together in a flat list, we could use an
|
||
|
association list like this:
|
||
|
</p>
|
||
|
<div class="example lisp">
|
||
|
<pre class="lisp-preformatted">(define car1-properties '((color . red)
|
||
|
(transmission . manual)
|
||
|
(fuel . unleaded)
|
||
|
(steering . power-assisted)))
|
||
|
</pre></div>
|
||
|
|
||
|
<p>Notice how this structure is more explicit and extensible than the flat
|
||
|
list. For example it makes clear that <code class="code">manual</code> refers to the
|
||
|
transmission rather than, say, the windows or the locking of the car.
|
||
|
It also allows further properties to use the same symbols among their
|
||
|
possible values without becoming ambiguous:
|
||
|
</p>
|
||
|
<div class="example lisp">
|
||
|
<pre class="lisp-preformatted">(define car1-properties '((color . red)
|
||
|
(transmission . manual)
|
||
|
(fuel . unleaded)
|
||
|
(steering . power-assisted)
|
||
|
(seat-color . red)
|
||
|
(locking . manual)))
|
||
|
</pre></div>
|
||
|
|
||
|
<p>With a representation like this, it is easy to use the efficient
|
||
|
<code class="code">assq-XXX</code> family of procedures (see <a class="pxref" href="Association-Lists.html">Association Lists</a>) to
|
||
|
extract or change individual pieces of information:
|
||
|
</p>
|
||
|
<div class="example lisp">
|
||
|
<pre class="lisp-preformatted">(assq-ref car1-properties 'fuel) ⇒ unleaded
|
||
|
(assq-ref car1-properties 'transmission) ⇒ manual
|
||
|
|
||
|
(assq-set! car1-properties 'seat-color 'black)
|
||
|
⇒
|
||
|
((color . red)
|
||
|
(transmission . manual)
|
||
|
(fuel . unleaded)
|
||
|
(steering . power-assisted)
|
||
|
(seat-color . black)
|
||
|
(locking . manual)))
|
||
|
</pre></div>
|
||
|
|
||
|
<p>Hash tables also have keys, and exactly the same arguments apply to the
|
||
|
use of symbols in hash tables as in association lists. The hash value
|
||
|
that Guile uses to decide where to add a symbol-keyed entry to a hash
|
||
|
table can be obtained by calling the <code class="code">symbol-hash</code> procedure:
|
||
|
</p>
|
||
|
<dl class="first-deffn">
|
||
|
<dt class="deffn" id="index-symbol_002dhash"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">symbol-hash</strong> <var class="def-var-arguments">symbol</var><a class="copiable-link" href="#index-symbol_002dhash"> ¶</a></span></dt>
|
||
|
<dt class="deffnx def-cmd-deffn" id="index-scm_005fsymbol_005fhash"><span class="category-def">C Function: </span><span><strong class="def-name">scm_symbol_hash</strong> <var class="def-var-arguments">(symbol)</var><a class="copiable-link" href="#index-scm_005fsymbol_005fhash"> ¶</a></span></dt>
|
||
|
<dd><p>Return a hash value for <var class="var">symbol</var>.
|
||
|
</p></dd></dl>
|
||
|
|
||
|
<p>See <a class="ref" href="Hash-Tables.html">Hash Tables</a> for information about hash tables in general, and
|
||
|
for why you might choose to use a hash table rather than an association
|
||
|
list.
|
||
|
</p>
|
||
|
|
||
|
</div>
|
||
|
<hr>
|
||
|
<div class="nav-panel">
|
||
|
<p>
|
||
|
Next: <a href="Symbol-Variables.html">Symbols as Denoting Variables</a>, Previous: <a href="Symbol-Data.html">Symbols as Discrete Data</a>, Up: <a href="Symbols.html">Symbols</a> [<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>
|