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

135 lines
8 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>Removing Alist Entries (Guile Reference Manual)</title>
<meta name="description" content="Removing Alist Entries (Guile Reference Manual)">
<meta name="keywords" content="Removing Alist Entries (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="Association-Lists.html" rel="up" title="Association Lists">
<link href="Sloppy-Alist-Functions.html" rel="next" title="Sloppy Alist Functions">
<link href="Retrieving-Alist-Entries.html" rel="prev" title="Retrieving Alist Entries">
<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="Removing-Alist-Entries">
<div class="nav-panel">
<p>
Next: <a href="Sloppy-Alist-Functions.html" accesskey="n" rel="next">Sloppy Alist Functions</a>, Previous: <a href="Retrieving-Alist-Entries.html" accesskey="p" rel="prev">Retrieving Alist Entries</a>, Up: <a href="Association-Lists.html" accesskey="u" rel="up">Association Lists</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="subsubsection" id="Removing-Alist-Entries-1"><span>6.6.20.4 Removing Alist Entries<a class="copiable-link" href="#Removing-Alist-Entries-1"> &para;</a></span></h4>
<p>To remove the element from an association list whose key matches a
specified key, use <code class="code">assq-remove!</code>, <code class="code">assv-remove!</code> or
<code class="code">assoc-remove!</code> (depending, as usual, on the level of equality
required between the key that you specify and the keys in the
association list).
</p>
<p>As with <code class="code">assq-set!</code> and friends, the specified alist may or may not
be modified destructively, and the only safe way to update a variable
containing the alist is to <code class="code">set!</code> it to the value that
<code class="code">assq-remove!</code> and friends return.
</p>
<div class="example">
<pre class="example-preformatted">address-list
&rArr;
((&quot;bob&quot; . &quot;11 Newington Avenue&quot;) (&quot;mary&quot; . &quot;34 Elm Road&quot;)
(&quot;james&quot; . &quot;1a London Road&quot;))
(set! address-list (assoc-remove! address-list &quot;mary&quot;))
address-list
&rArr;
((&quot;bob&quot; . &quot;11 Newington Avenue&quot;) (&quot;james&quot; . &quot;1a London Road&quot;))
</pre></div>
<p>Note that, when <code class="code">assq/v/oc-remove!</code> is used to modify an
association list that has been constructed only using the corresponding
<code class="code">assq/v/oc-set!</code>, there can be at most one matching entry in the
alist, so the question of multiple entries being removed in one go does
not arise. If <code class="code">assq/v/oc-remove!</code> is applied to an association
list that has been constructed using <code class="code">acons</code>, or an
<code class="code">assq/v/oc-set!</code> with a different level of equality, or any mixture
of these, it removes only the first matching entry from the alist, even
if the alist might contain further matching entries. For example:
</p>
<div class="example">
<pre class="example-preformatted">(define address-list '())
(set! address-list (assq-set! address-list &quot;mary&quot; &quot;11 Elm Street&quot;))
(set! address-list (assq-set! address-list &quot;mary&quot; &quot;57 Pine Drive&quot;))
address-list
&rArr;
((&quot;mary&quot; . &quot;57 Pine Drive&quot;) (&quot;mary&quot; . &quot;11 Elm Street&quot;))
(set! address-list (assoc-remove! address-list &quot;mary&quot;))
address-list
&rArr;
((&quot;mary&quot; . &quot;11 Elm Street&quot;))
</pre></div>
<p>In this example, the two instances of the string &quot;mary&quot; are not the same
when compared using <code class="code">eq?</code>, so the two <code class="code">assq-set!</code> calls add
two distinct entries to <code class="code">address-list</code>. When compared using
<code class="code">equal?</code>, both &quot;mary&quot;s in <code class="code">address-list</code> are the same as the
&quot;mary&quot; in the <code class="code">assoc-remove!</code> call, but <code class="code">assoc-remove!</code> stops
after removing the first matching entry that it finds, and so one of the
&quot;mary&quot; entries is left in place.
</p>
<dl class="first-deffn">
<dt class="deffn" id="index-assq_002dremove_0021"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">assq-remove!</strong> <var class="def-var-arguments">alist key</var><a class="copiable-link" href="#index-assq_002dremove_0021"> &para;</a></span></dt>
<dt class="deffnx def-cmd-deffn" id="index-assv_002dremove_0021"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">assv-remove!</strong> <var class="def-var-arguments">alist key</var><a class="copiable-link" href="#index-assv_002dremove_0021"> &para;</a></span></dt>
<dt class="deffnx def-cmd-deffn" id="index-assoc_002dremove_0021"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">assoc-remove!</strong> <var class="def-var-arguments">alist key</var><a class="copiable-link" href="#index-assoc_002dremove_0021"> &para;</a></span></dt>
<dt class="deffnx def-cmd-deffn" id="index-scm_005fassq_005fremove_005fx"><span class="category-def">C Function: </span><span><strong class="def-name">scm_assq_remove_x</strong> <var class="def-var-arguments">(alist, key)</var><a class="copiable-link" href="#index-scm_005fassq_005fremove_005fx"> &para;</a></span></dt>
<dt class="deffnx def-cmd-deffn" id="index-scm_005fassv_005fremove_005fx"><span class="category-def">C Function: </span><span><strong class="def-name">scm_assv_remove_x</strong> <var class="def-var-arguments">(alist, key)</var><a class="copiable-link" href="#index-scm_005fassv_005fremove_005fx"> &para;</a></span></dt>
<dt class="deffnx def-cmd-deffn" id="index-scm_005fassoc_005fremove_005fx"><span class="category-def">C Function: </span><span><strong class="def-name">scm_assoc_remove_x</strong> <var class="def-var-arguments">(alist, key)</var><a class="copiable-link" href="#index-scm_005fassoc_005fremove_005fx"> &para;</a></span></dt>
<dd><p>Delete the first entry in <var class="var">alist</var> associated with <var class="var">key</var>, and return
the resulting alist.
</p></dd></dl>
</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="Sloppy-Alist-Functions.html">Sloppy Alist Functions</a>, Previous: <a href="Retrieving-Alist-Entries.html">Retrieving Alist Entries</a>, Up: <a href="Association-Lists.html">Association Lists</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>