⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit-data-structures.html

📁 Scheme跨平台编译器
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<html><head><title>CHICKEN User's Manual - Unit data-structures</title></head><body><p> </p><a name="unit-data-structures"></a><h1>Unit data-structures</h1><p>This unit contains a collection of procedures related to data structures.  This unit is used by default, unless the program is compiled with the <tt>-explicit-use</tt> option.</p><a name="lists"></a><h2>Lists</h2><a name="alist-ref"></a><h3>alist-ref</h3><pre>[procedure] (alist-ref KEY ALIST [TEST [DEFAULT]])</pre><p>Looks up <tt>KEY</tt> in <tt>ALIST</tt> using <tt>TEST</tt> as the comparison function (or <tt>eqv?</tt> if no test was given) and returns the cdr of the found pair, or <tt>DEFAULT</tt> (which defaults to <tt>#f</tt>).</p><a name="alist-update"></a><h3>alist-update!</h3><pre>[procedure] (alist-update! KEY VALUE ALIST [TEST])</pre><p>If the list <tt>ALIST</tt> contains a pair of the form <tt>(KEY . X)</tt>, then this procedure replaces <tt>X</tt> with <tt>VALUE</tt> and returns <tt>ALIST</tt>. If <tt>ALIST</tt> contains no such item, then <tt>alist-update!</tt> returns <tt>((KEY . VALUE) . ALIST)</tt>. The optional argument <tt>TEST</tt> specifies the comparison procedure to search a matching pair in <tt>ALIST</tt> and defaults to <tt>eqv?</tt>.</p><a name="atom"></a><h3>atom?</h3><pre>[procedure] (atom? X)</pre><p>Returns <tt>#t</tt> if <tt>X</tt> is not a pair. This is identical to <tt>not-pair?</tt> from <a href="unit-srfi-1.html" class="internal">Unit srfi-1</a> but kept for historical reasons.</p><a name="rassoc"></a><h3>rassoc</h3><pre>[procedure] (rassoc KEY LIST [TEST])</pre><p>Similar to <tt>assoc</tt>, but compares <tt>KEY</tt> with the <tt>cdr</tt> of each pair in <tt>LIST</tt> using <tt>TEST</tt> as the comparison procedures (which defaults to <tt>eqv?</tt>.</p><a name="butlast"></a><h3>butlast</h3><pre>[procedure] (butlast LIST)</pre><p>Returns a fresh list with all elements but the last of <tt>LIST</tt>.</p><a name="chop"></a><h3>chop</h3><pre>[procedure] (chop LIST N)</pre><p>Returns a new list of sublists, where each sublist contains <tt>N</tt> elements of <tt>LIST</tt>. If <tt>LIST</tt> has a length that is not a multiple of <tt>N</tt>, then the last sublist contains the remaining elements.</p><PRE>(chop '(1 2 3 4 5 6) 2) =<B><FONT COLOR="#A020F0">=&gt;</FONT></B> ((1 2) (3 4) (5 6))(chop '(a b c d) 3)     =<B><FONT COLOR="#A020F0">=&gt;</FONT></B> ((a b c) (d))</PRE><a name="compress"></a><h3>compress</h3><pre>[procedure] (compress BLIST LIST)</pre><p>Returns a new list with elements taken from <tt>LIST</tt> with corresponding true values in the list <tt>BLIST</tt>.</p><PRE>(<B><FONT COLOR="#A020F0">define</FONT></B> <B><FONT COLOR="#0000FF">nums</FONT></B> '(99 100 110 401 1234))(compress (map odd? nums) nums)      =<B><FONT COLOR="#A020F0">=&gt;</FONT></B> (99 401)</PRE><a name="flatten"></a><h3>flatten</h3><pre>[procedure] (flatten LIST1 ...)</pre><p>Returns <tt>LIST1 ...</tt> concatenated together, with nested lists removed (flattened).</p><a name="intersperse"></a><h3>intersperse</h3><pre>[procedure] (intersperse LIST X)</pre><p>Returns a new list with <tt>X</tt> placed between each element.</p><a name="join"></a><h3>join</h3><pre>[procedure] (join LISTOFLISTS [LIST])</pre><p>Concatenates the lists in <tt>LISTOFLISTS</tt> with <tt>LIST</tt> placed between each sublist. <tt>LIST</tt> defaults to the empty list.</p><PRE>(join '((a b) (c d) (e)) '(x y)) =<B><FONT COLOR="#A020F0">=&gt;</FONT></B> (a b x y c d x y e)(join '((p q) () (r (s) t)) '(-))  =<B><FONT COLOR="#A020F0">=&gt;</FONT></B> (p q - - r (s) t)</PRE><p><tt>join</tt> could be implemented as follows:</p><PRE>(<B><FONT COLOR="#A020F0">define</FONT></B> (<B><FONT COLOR="#0000FF">join</FONT></B> lstoflsts #!optional (lst '()))  (apply append (intersperse lstoflists lst)) )</PRE><a name="shuffle"></a><h3>shuffle</h3><pre>[procedure] (shuffle LIST RANDOM)</pre><p>Returns <tt>LIST</tt> with its elements sorted in a random order given by procedure RANDOM. </p><a name="tail"></a><h3>tail?</h3><pre>[procedure] (tail? X LIST)</pre><p>Returns true if <tt>X</tt> is one of the tails (cdr's) of <tt>LIST</tt>.</p><a name="queues"></a><h2>Queues</h2><a name="list-queue"></a><h3>list&rarr;queue</h3><pre>[procedure] (list-&gt;queue LIST)</pre><p>Returns <tt>LIST</tt> converted into a queue, where the first element of the list is the same as the first element of the queue. The resulting queue may share memory with the list and the list should not be modified after this operation.</p><a name="make-queue"></a><h3>make-queue</h3><pre>[procedure] (make-queue)</pre><p>Returns a newly created queue.</p><a name="queue"></a><h3>queue?</h3><pre>[procedure] (queue? X)</pre><p>Returns <tt>#t</tt> if <tt>X</tt> is a queue, or <tt>#f</tt> otherwise.</p><a name="queue-list"></a><h3>queue&rarr;list</h3><pre>[procedure] (queue-&gt;list QUEUE)</pre><p>Returns <tt>QUEUE</tt> converted into a list, where the first element of the list is the same as the first element of the queue. The resulting list may share memory with the queue object and should not be modified.</p><a name="queue-add"></a><h3>queue-add!</h3><pre>[procedure] (queue-add! QUEUE X)</pre><p>Adds <tt>X</tt> to the rear of <tt>QUEUE</tt>.</p><a name="queue-empty"></a><h3>queue-empty?</h3><pre>[procedure] (queue-empty? QUEUE)</pre><p>Returns <tt>#t</tt> if <tt>QUEUE</tt> is empty, or <tt>#f</tt> otherwise.</p><a name="queue-first"></a><h3>queue-first</h3><pre>[procedure] (queue-first QUEUE)</pre><p>Returns the first element of <tt>QUEUE</tt>. If <tt>QUEUE</tt> is empty an error is signaled</p><a name="queue-last"></a><h3>queue-last</h3><pre>[procedure] (queue-last QUEUE)</pre><p>Returns the last element of <tt>QUEUE</tt>. If <tt>QUEUE</tt> is empty an error is signaled</p><a name="queue-remove"></a><h3>queue-remove!</h3><pre>[procedure] (queue-remove! QUEUE)</pre><p>Removes and returns the first element of <tt>QUEUE</tt>. If <tt>QUEUE</tt> is empty an error is signaled</p><a name="queue-push-back"></a><h3>queue-push-back!</h3><pre>[procedure] (queue-push-back! QUEUE ITEM)</pre><p>Pushes an item into the first position of a queue, i.e. the next <tt>queue-remove!</tt> will return <tt>ITEM</tt>.</p><a name="queue-push-back-list"></a><h3>queue-push-back-list!</h3><pre>[procedure] (queue-push-back-list! QUEUE LIST)</pre><p>Pushes the items in item-list back onto the queue, so that <tt>(car LIST)</tt> becomes the next removable item.</p><a name="sorting"></a><h2>Sorting</h2><a name="merge"></a><h3>merge</h3><pre>[procedure] (merge LIST1 LIST2 LESS?)[procedure] (merge! LIST1 LIST2 LESS?)</pre><p>Joins two lists in sorted order. <tt>merge!</tt> is the destructive version of merge. <tt>LESS?  </tt> should be a procedure of two arguments, that returns true if the first argument is to be ordered before the second argument.</p><a name="sort"></a><h3>sort</h3><pre>[procedure] (sort SEQUENCE LESS?)[procedure] (sort! SEQUENCE LESS?)</pre><p>Sort <tt>SEQUENCE</tt>, which should be a list or a vector. <tt>sort!</tt> is the destructive version of sort.</p><a name="sorted"></a><h3>sorted?</h3><pre>[procedure] (sorted? SEQUENCE LESS?)</pre><p>Returns true if the list or vector <tt>SEQUENCE</tt> is already sorted.</p><a name="random-numbers"></a><h2>Random numbers</h2><a name="random-seed"></a><h3>random-seed</h3><pre>[procedure] (random-seed [SEED])</pre><p>Seeds the random number generator with <tt>SEED</tt> (an exact integer) or  <tt>(current-seconds)</tt> if <tt>SEED</tt> is not given.</p><a name="strings"></a><h2>Strings</h2><a name="conc"></a><h3>conc</h3><pre>[procedure] (conc X ...)</pre><p>Returns a string with the string-represenation of all arguments concatenated together. <tt>conc</tt> could be implemented as</p><PRE>(<B><FONT COLOR="#A020F0">define</FONT></B> (<B><FONT COLOR="#0000FF">conc</FONT></B> . args)  (apply string-append (map -&gt;string args)) )</PRE><a name="string"></a><h3>&rarr;string</h3><pre>[procedure] (-&gt;string X)</pre><p>Returns a string-representation of <tt>X</tt>.</p><a name="string-chop"></a><h3>string-chop</h3><pre>[procedure] (string-chop STRING LENGTH)</pre><p>Returns a list of substrings taken by <em>chopping</em> <tt>STRING</tt> every <tt>LENGTH</tt> characters:</p><PRE>(string-chop <B><FONT COLOR="#BC8F8F">&quot;one two three&quot;</FONT></B> 4)  =<B><FONT COLOR="#A020F0">=&gt;</FONT></B>  (<B><FONT COLOR="#BC8F8F">&quot;one &quot;</FONT></B> <B><FONT COLOR="#BC8F8F">&quot;two &quot;</FONT></B> <B><FONT COLOR="#BC8F8F">&quot;thre&quot;</FONT></B> <B><FONT COLOR="#BC8F8F">&quot;e&quot;</FONT></B>)</PRE><a name="string-chomp"></a><h3>string-chomp</h3><pre>[procedure] (string-chomp STRING [SUFFIX])</pre><p>If <tt>STRING</tt> ends with <tt>SUFFIX</tt>, then this procedure returns a copy of its first argument with the suffix removed, otherwise returns <tt>STRING</tt> unchanged. <tt>SUFFIX</tt> defaults to <tt>"\n"</tt>.</p><a name="string-compare3"></a><h3>string-compare3</h3><pre>[procedure] (string-compare3 STRING1 STRING2)[procedure] (string-compare3-ci STRING1 STRING2)</pre><p>Perform a three-way comparison between the <tt>STRING1</tt> and <tt>STRING2</tt>, returning either <tt>-1</tt> if <tt>STRING1</tt> is lexicographically less than <tt>STRING2</tt>, <tt>0</tt> if it is equal, or <tt>1</tt> if it s greater. <tt>string-compare3-ci</tt> performs a case-insensitive comparison.</p><a name="string-intersperse"></a><h3>string-intersperse</h3><pre>[procedure] (string-intersperse LIST [STRING])</pre><p>Returns a string that contains all strings in <tt>LIST</tt> concatenated together.  <tt>STRING</tt> is placed between each concatenated string and defaults to <tt>" "</tt>.</p><PRE>(string-intersperse '(<B><FONT COLOR="#BC8F8F">&quot;one&quot;</FONT></B> <B><FONT COLOR="#BC8F8F">&quot;two&quot;</FONT></B>) <B><FONT COLOR="#BC8F8F">&quot;three&quot;</FONT></B>)</PRE><p>is equivalent to</p><PRE>(apply string-append (intersperse '(<B><FONT COLOR="#BC8F8F">&quot;one&quot;</FONT></B> <B><FONT COLOR="#BC8F8F">&quot;two&quot;</FONT></B>) <B><FONT COLOR="#BC8F8F">&quot;three&quot;</FONT></B>))</PRE><a name="string-split"></a><h3>string-split</h3><pre>[procedure] (string-split STRING [DELIMITER-STRING [KEEPEMPTY]])</pre><p>Split string into substrings separated by the given delimiters. If no delimiters are specified, a string comprising the tab, newline and space characters  is assumed. If the parameter <tt>KEEPEMPTY</tt> is given and not <tt>#f</tt>, then empty substrings are retained:</p><PRE>(string-split <B><FONT COLOR="#BC8F8F">&quot;one  two  three&quot;</FONT></B>) =<B><FONT COLOR="#A020F0">=&gt;</FONT></B> (<B><FONT COLOR="#BC8F8F">&quot;one&quot;</FONT></B> <B><FONT COLOR="#BC8F8F">&quot;two&quot;</FONT></B> <B><FONT COLOR="#BC8F8F">&quot;three&quot;</FONT></B>)(string-split <B><FONT COLOR="#BC8F8F">&quot;foo:bar::baz:&quot;</FONT></B> <B><FONT COLOR="#BC8F8F">&quot;:&quot;</FONT></B> #t) =<B><FONT COLOR="#A020F0">=&gt;</FONT></B> (<B><FONT COLOR="#BC8F8F">&quot;foo&quot;</FONT></B> <B><FONT COLOR="#BC8F8F">&quot;bar&quot;</FONT></B> <B><FONT COLOR="#BC8F8F">&quot;&quot;</FONT></B> <B><FONT COLOR="#BC8F8F">&quot;baz&quot;</FONT></B> <B><FONT COLOR="#BC8F8F">&quot;&quot;</FONT></B>)</PRE><a name="string-translate"></a><h3>string-translate</h3><pre>[procedure] (string-translate STRING FROM [TO])</pre><p>Returns a fresh copy of <tt>STRING</tt> with characters matching <tt>FROM</tt> translated to <tt>TO</tt>.  If <tt>TO</tt> is omitted, then matching characters are removed. <tt>FROM</tt> and <tt>TO</tt> may be a character, a string or a list. If both <tt>FROM</tt> and <tt>TO</tt> are strings, then the character at the same position in <tt>TO</tt> as the matching character in <tt>FROM</tt> is substituted.</p><a name="string-translate"></a><h3>string-translate*</h3><pre>[procedure] (string-translate* STRING SMAP)</pre><p>Substitutes elements of <tt>STRING</tt> according to <tt>SMAP</tt>. <tt>SMAP</tt> should be an association-list where each element of the list is a pair of the form <tt>(MATCH \. REPLACEMENT)</tt>. Every occurrence of the string <tt>MATCH</tt> in <tt>STRING</tt> will be replaced by the string <tt>REPLACEMENT</tt>:</p><PRE>(string-translate*  <B><FONT COLOR="#BC8F8F">&quot;&lt;h1&gt;this is a \&quot;string\&quot;&lt;/h1&gt;&quot;</FONT></B>  '((<B><FONT COLOR="#BC8F8F">&quot;&lt;&quot;</FONT></B> . <B><FONT COLOR="#BC8F8F">&quot;&amp;lt;&quot;</FONT></B>) (<B><FONT COLOR="#BC8F8F">&quot;&gt;&quot;</FONT></B> . <B><FONT COLOR="#BC8F8F">&quot;&amp;gt;&quot;</FONT></B>) (<B><FONT COLOR="#BC8F8F">&quot;\&quot;&quot;</FONT></B> . <B><FONT COLOR="#BC8F8F">&quot;&amp;quot;&quot;</FONT></B>)) )

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -