<h1id="title-non-xs"><ahref="index.html">The Common Lisp Cookbook</a>– Miscellaneous</h1>
<!-- Announcement we can keep for 1 month or more. I remove it and re-add it from time to time. -->
<pclass="announce">
📹 <ahref="https://www.udemy.com/course/common-lisp-programming/?couponCode=6926D599AA-LISP4ALL">NEW! Learn Lisp in videos and support our contributors with this 40% discount.</a>
</p>
<pclass="announce-neutral">
📕 <ahref="index.html#download-in-epub">Get the EPUB and PDF</a>
</p>
<divid="content"
<p><aname="opt"></a></p>
<h2id="re-using-complex-data-structures">Re-using complex data structures</h2>
<p>Sometimes you want your functions to behave in a ‘functional’ way, i.e. return <ahref="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_f.htm#fresh">fresh</a> results without side effects, sometimes you want them to re-use and modify existing data in a destructive way - consider the difference between <ahref="http://www.lispworks.com/documentation/HyperSpec/Body/f_append.htm"><code>append</code></a> and <ahref="http://www.lispworks.com/documentation/HyperSpec/Body/f_nconc.htm"><code>nconc</code></a> for an example.</p>
<p>Well, you can have your cake and eat it too, by using optional (or keyword) parameters. Here’s an example: Let’s assume you’re writing a function <code>complex-matrix-stuff</code> which takes two matrices <code>m1</code> and <code>m2</code> as its arguments and computes and returns a resulting matrix the size of which depends on <code>m1</code> and <code>m2</code>, i.e. for a fresh result you’ll need an empty matrix which’ll be created by, say, <code>(make-appropriate-result-matrix-for m1 m2)</code>.</p>
<p>The classical textbook way to implement this function will more or less look like this:</p>
<h2id="using-adjust-array-instead-of-consing-up-new-sequences-with-subseq">Using <code>ADJUST-ARRAY</code> instead of consing up new sequences with <code>SUBSEQ</code></h2>
<p>Most CL functions operating on sequences will accept <code>start</code> and <code>end</code> keywords so you can make them operate on a sub-sequence without actually creating it, i.e. instead of</p>
<pre><codeclass="language-lisp">(count #\a (subseq long-string from to))
</code></pre>
<p>you should of course use</p>
<pre><codeclass="language-lisp">(count #\a long-string :start from :end to)
</code></pre>
<p>which’ll yield the same result but not create an unnecessary intermediate sub-sequence.</p>
<p>However, sometimes it looks like you can’t avoid creating new data. Consider a hash table the keys of which are strings. If the key you’re looking for is a sub-string of another string you’ll most likely end up with</p>
<pre><codeclass="language-lisp">(gethash (subseq original-string from to)
hash-table)
</code></pre>
<p>But you don’t have to. You can create <em>one</em><ahref="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_d.htm#displaced_array">displaced</a> string and reuse it multiple times with <ahref="http://www.lispworks.com/documentation/HyperSpec/Body/f_adjust.htm"><code>adjust-array</code></a>:</p>