📄 unit-library.html
字号:
<html><head><title>CHICKEN User's Manual - Unit library</title></head><body><p> </p><a name="unit-library"></a><h1>Unit library</h1><p>This unit contains basic Scheme definitions. This unit is used by default, unless the program is compiled with the <tt>-explicit-use</tt> option.</p><a name="arithmetic"></a><h2>Arithmetic</h2><a name="add1-sub1"></a><h3>add1/sub1</h3><dl><dt>[procedure] (add1 N)</dt><dt>[procedure] (sub1 N)</dt></dl><p>Adds/subtracts 1 from <tt>N</tt>.</p><a name="binary-integer-operations"></a><h3>Binary integer operations</h3><p>Binary integer operations. <tt>arithmetic-shift</tt> shifts the argument <tt>N1</tt> by <tt>N2</tt> bits to the left. If <tt>N2</tt> is negative, than <tt>N1</tt> is shifted to the right. These operations only accept exact integers or inexact integers in word range (32 bit signed on 32-bit platforms, or 64 bit signed on 64-bit platforms).</p><dl><dt>[procedure] (bitwise-and N1 ...)</dt><dt>[procedure] (bitwise-ior N1 ...)</dt><dt>[procedure] (bitwise-xor N1 ...)</dt><dt>[procedure] (bitwise-not N)</dt><dt>[procedure] (arithmetic-shift N1 N2)</dt></dl><a name="bit-set"></a><h3>bit-set?</h3><pre>[procedure] (bit-set? N INDEX)</pre><p>Returns <tt>#t</tt> if the bit at the position <tt>INDEX</tt> in the integer <tt>N</tt> is set, or <tt>#f</tt> otherwise. The rightmost/least-significant bit is bit 0.</p><a name="fixnum"></a><h3>fixnum?</h3><pre>[procedure] (fixnum? X)</pre><p>Returns <tt>#t</tt> if <tt>X</tt> is a fixnum, or <tt>#f</tt> otherwise.</p><a name="arithmetic-fixnum-operations"></a><h3>Arithmetic fixnum operations</h3><p>These procedures do not check their arguments, so non-fixnum parameters will result in incorrect results. <tt>fxneg</tt> negates its argument.</p><p>On division by zero, <tt>fx/</tt> and <tt>fxmod</tt> signal a condition of kind <tt>(exn arithmetic)</tt>.</p><p><tt>fxshl</tt> and <tt>fxshr</tt> perform arithmetic shift left and right, respectively.</p><dl><dt>[procedure] (fx+ N1 N2)</dt><dt>[procedure] (fx- N1 N2)</dt><dt>[procedure] (fx* N1 N2)</dt><dt>[procedure] (fx/ N1 N2)</dt><dt>[procedure] (fxmod N1 N2)</dt><dt>[procedure] (fxneg N)</dt><dt>[procedure] (fxmin N1 N2)</dt><dt>[procedure] (fxmax N1 N2)</dt><dt>[procedure] (fx= N1 N2)</dt><dt>[procedure] (fx> N1 N2)</dt><dt>[procedure] (fx< N1 N2)</dt><dt>[procedure] (fx>= N1 N2)</dt><dt>[procedure] (fx⇐ N1 N2)</dt><dt>[procedure] (fxand N1 N2)</dt><dt>[procedure] (fxior N1 N2)</dt><dt>[procedure] (fxxor N1 N2)</dt><dt>[procedure] (fxnot N)</dt><dt>[procedure] (fxshl N1 N2)</dt><dt>[procedure] (fxshr N1 N2)</dt></dl><a name="arithmetic-floating-point-operations"></a><h3>Arithmetic floating-point operations</h3><p>In safe mode, these procedures throw a type error with non-float arguments (except <tt>flonum?</tt>, which returns <tt>#f</tt>). In unsafe mode, these procedures do not check their arguments. A non-flonum argument in unsafe mode can crash the system.</p><dl><dt>[procedure] (flonum? X)</dt><dt>[procedure] (fp+ X Y)</dt><dt>[procedure] (fp- X Y)</dt><dt>[procedure] (fp* X Y)</dt><dt>[procedure] (fp/ X Y)</dt><dt>[procedure] (fpneg X)</dt><dt>[procedure] (fpmin X Y)</dt><dt>[procedure] (fpmax X Y)</dt><dt>[procedure] (fp= X Y)</dt><dt>[procedure] (fp> X Y)</dt><dt>[procedure] (fp< X Y)</dt><dt>[procedure] (fp>= X Y)</dt><dt>[procedure] (fp⇐ X Y)</dt></dl><a name="signum"></a><h3>signum</h3><pre>[procedure] (signum N)</pre><p>Returns <tt>1</tt> if <tt>N</tt> is positive, <tt>-1</tt> if <tt>N</tt> is negative or <tt>0</tt> if <tt>N</tt> is zero. <tt>signum</tt> is exactness preserving.</p><a name="finite"></a><h3>finite?</h3><pre>[procedure] (finite? N)</pre><p>Returns <tt>#f</tt> if <tt>N</tt> is negative or positive infinity, and <tt>#t</tt> otherwise.</p><a name="file-input-output"></a><h2>File Input/Output</h2><a name="current-output-port"></a><h3>current-output-port</h3><pre>[procedure] (current-output-port [PORT])</pre><p>Returns default output port. If <tt>PORT</tt> is given, then that port is selected as the new current output port.</p><p>Note that the default output port is not buffered. Use [[<a href="http://galinha.ucpel.tche.br/Unit" class="external">http://galinha.ucpel.tche.br/Unit</a> posix#Setting the file buffering mode|<tt>set-buffering-mode!</tt>]] if you need a different behavior.</p><a name="current-error-port"></a><h3>current-error-port</h3><pre>[procedure] (current-error-port [PORT])</pre><p>Returns default error output port. If <tt>PORT</tt> is given, then that port is selected as the new current error output port.</p><p>Note that the default error output port is not buffered. Use [[<a href="http://galinha.ucpel.tche.br/Unit" class="external">http://galinha.ucpel.tche.br/Unit</a> posix#Setting the file buffering mode|<tt>set-buffering-mode!</tt>]] if you need a different behavior.</p><a name="flush-output"></a><h3>flush-output</h3><pre>[procedure] (flush-output [PORT])</pre><p>Write buffered output to the given output-port. <tt>PORT</tt> defaults to the value of <tt>(current-output-port)</tt>.</p><a name="port-name"></a><h3>port-name</h3><pre>[procedure] (port-name [PORT])</pre><p>Fetch filename from <tt>PORT</tt>. This returns the filename that was used to open this file. Returns a special tag string, enclosed into parentheses for non-file ports. <tt>PORT</tt> defaults to the value of <tt>(current-input-port)</tt>.</p><a name="port-position"></a><h3>port-position</h3><pre>[procedure] (port-position [PORT])</pre><p>Returns the current position of <tt>PORT</tt> as two values: row and column number. If the port does not support such an operation an error is signaled. This procedure is currently only available for input ports. <tt>PORT</tt> defaults to the value of <tt>(current-input-port)</tt>.</p><a name="set-port-name"></a><h3>set-port-name!</h3><pre>[procedure] (set-port-name! PORT STRING)</pre><p>Sets the name of <tt>PORT</tt> to <tt>STRING</tt>.</p><a name="files"></a><h2>Files</h2><a name="delete-file"></a><h3>delete-file</h3><pre>[procedure] (delete-file STRING)</pre><p>Deletes the file with the pathname <tt>STRING</tt>. If the file does not exist, an error is signaled.</p><a name="file-exists"></a><h3>file-exists?</h3><pre>[procedure] (file-exists? STRING)</pre><p>Returns <tt>STRING</tt> if a file with the given pathname exists, or <tt>#f</tt> otherwise.</p><a name="rename-file"></a><h3>rename-file</h3><pre>[procedure] (rename-file OLD NEW)</pre><p>Renames the file or directory with the pathname <tt>OLD</tt> to <tt>NEW</tt>. If the operation does not succeed, an error is signaled.</p><a name="string-ports"></a><h2>String ports</h2><a name="get-output-string"></a><h3>get-output-string</h3><pre>[procedure] (get-output-string PORT)</pre><p>Returns accumulated output of a port created with <tt>(open-output-string)</tt>.</p><a name="open-input-string"></a><h3>open-input-string</h3><pre>[procedure] (open-input-string STRING)</pre><p>Returns a port for reading from <tt>STRING</tt>.</p><a name="open-output-string"></a><h3>open-output-string</h3><pre>[procedure] (open-output-string)</pre><p>Returns a port for accumulating output in a string.</p><a name="feature-identifiers"></a><h2>Feature identifiers</h2><p>CHICKEN maintains a global list of <em>features</em> naming functionality available in the current system. Additionally the <tt>cond-expand</tt> form accesses this feature list to infer what features are provided. Predefined features are <tt>chicken</tt>, and the SRFIs (Scheme Request For Implementation) provided by the base system: <tt>srfi-23, srfi-30, srfi-39</tt>. If the <tt>eval</tt> unit is used (the default), the features <tt>srfi-0, srfi-2, srfi-6, srfi-8, srfi-9</tt> and <tt>srfi-10</tt> are defined. When compiling code (during compile-time) the feature <tt>compiling</tt> is registered. When evaluating code in the interpreter (csi), the feature <tt>csi</tt> is registered.</p><a name="features"></a><h3>features</h3><pre>[procedure] (features)</pre><p>Returns a list of all registered features that will be accepted as valid feature-identifiers by <tt>cond-expand</tt>.</p><a name="feature"></a><h3>feature?</h3><pre>[procedure] (feature? ID ...)</pre><p>Returns <tt>#t</tt> if all features with the given feature-identifiers <tt>ID ...</tt> are registered.</p><a name="register-feature"></a><h3>register-feature!</h3><pre>[procedure] (register-feature! FEATURE ...)</pre><p>Register one or more features that will be accepted as valid feature-identifiers by <tt>cond-expand</tt>. <tt>FEATURE ...</tt> may be a keyword, string or symbol.</p><a name="unregister-feature"></a><h3>unregister-feature!</h3><pre>[procedure] (unregister-feature! FEATURE ...)</pre><p>Unregisters the specified feature-identifiers. <tt>FEATURE ...</tt> may be a keyword, string or symbol.</p><a name="keywords"></a><h2>Keywords</h2><p>Keywords are special symbols prefixed with <tt>#:</tt> that evaluate to themselves. Procedures can use keywords to accept optional named parameters in addition to normal required parameters. Assignment to and bindings of keyword symbols is not allowed. The parameter <tt>keyword-style</tt> and the compiler/interpreter option <tt>-keyword-style</tt> can be used to allow an additional keyword syntax, either compatible to Common LISP, or to DSSSL.</p><a name="get-keyword"></a><h3>get-keyword</h3><pre>[procedure] (get-keyword KEYWORD ARGLIST [THUNK])</pre><p>Returns the argument from <tt>ARGLIST</tt> specified under the keyword <tt>KEYWORD</tt>. If the keyword is not found, then the zero-argument procedure <tt>THUNK</tt> is invoked and the result value is returned. If <tt>THUNK</tt> is not given, <tt>#f</tt> is returned.</p><PRE>(<B><FONT COLOR="#A020F0">define</FONT></B> (<B><FONT COLOR="#0000FF">increase</FONT></B> x . args) (+ x (get-keyword #:amount args (<B><FONT COLOR="#A020F0">lambda</FONT></B> () 1))) )(increase 123) =<B><FONT COLOR="#A020F0">=></FONT></B> 124(increase 123 #:amount 10) =<B><FONT COLOR="#A020F0">=></FONT></B> 133</PRE><p>Note: the <tt>KEYWORD</tt> may actually be any kind of object.</p><a name="keyword"></a><h3>keyword?</h3><pre>[procedure] (keyword? X)</pre><p>Returns <tt>#t</tt> if <tt>X</tt> is a keyword symbol, or <tt>#f</tt> otherwise.</p><a name="keyword-string"></a><h3>keyword→string</h3><pre>[procedure] (keyword->string KEYWORD)</pre><p>Transforms <tt>KEYWORD</tt> into a string.</p><a name="string-keyword"></a><h3>string→keyword</h3><pre>[procedure] (string->keyword STRING)</pre><p>Returns a keyword with the name <tt>STRING</tt>.</p><a name="exceptions"></a><h2>Exceptions</h2><p>CHICKEN implements the (currently withdrawn) <a href="http://srfi.schemers.org/srfi-12/srfi-12.html" class="external">SRFI-12</a> exception system. For more information, see the <a href="http://srfi.schemers.org/srfi-12/srfi-12.html" class="external">SRFI-12</a> document.</p><a name="condition-case"></a><h3>condition-case</h3><pre>[syntax] (condition-case EXPRESSION CLAUSE ...)</pre><p>Evaluates <tt>EXPRESSION</tt> and handles any exceptions that are covered by <tt>CLAUSE ...</tt>, where <tt>CLAUSE</tt> should be of the following form:</p><PRE>CLAUSE = ([VARIABLE] (KIND ...) BODY ...)</PRE><p>If provided, <tt>VARIABLE</tt> will be bound to the signaled exception object. <tt>BODY ...</tt> is executed when the exception is a property- or composite condition with the kinds given <tt>KIND ...</tt> (unevaluated). If no clause applies, the exception is re-signaled in the same dynamic context as the <tt>condition-case</tt> form.</p><PRE>(<B><FONT COLOR="#A020F0">define</FONT></B> (<B><FONT COLOR="#0000FF">check</FONT></B> thunk) (condition-case (thunk) [(exn file) (print <B><FONT COLOR="#BC8F8F">"file error"</FONT></B>)] [(exn) (print <B><FONT COLOR="#BC8F8F">"other error"</FONT></B>)] [var () (print <B><FONT COLOR="#BC8F8F">"something else"</FONT></B>)] ) )(check (<B><FONT COLOR="#A020F0">lambda</FONT></B> () (open-input-file <B><FONT COLOR="#BC8F8F">""</FONT></B>))) <I><FONT COLOR="#B22222">; -> "file error"</FONT></I>(check (<B><FONT COLOR="#A020F0">lambda</FONT></B> () some-unbound-variable)) <I><FONT COLOR="#B22222">; -> "othererror"</FONT></I>(check (<B><FONT COLOR="#A020F0">lambda</FONT></B> () (signal 99))) <I><FONT COLOR="#B22222">; -> "something else"</FONT></I>(condition-case some-unbound-variable [(exn file) (print <B><FONT COLOR="#BC8F8F">"ignored"</FONT></B>)] ) <I><FONT COLOR="#B22222">; -> signals error</FONT></I></PRE><a name="breakpoint"></a><h3>breakpoint</h3><pre>[procedure] (breakpoint [NAME])</pre><p>Programmatically triggers a breakpoint (similar to the <tt>,br</tt> top-level csi command).</p><p>All error-conditions signaled by the system are of kind <tt>exn</tt>. The following composite conditions are additionally defined:</p><table><tr><td><pre>(exn arity)</pre><p>Signaled when a procedure is called with the wrong number of arguments.</p></td></tr><tr><td><pre>(exn type)</pre><p>Signaled on type-mismatch errors, for example when an argument of the wrong type is passed to a built-in procedure.</p></td></tr><tr><td><pre>(exn arithmetic)</pre><p>Signaled on arithmetic errors, like division by zero.</p></td></tr><tr><td><pre>(exn i/o)</pre><p>Signaled on input/output errors.</p></td></tr><tr><td><pre>(exn i/o file)</pre><p>Signaled on file-related errors.</p></td></tr><tr><td><pre>(exn i/o net)</pre><p>Signaled on network errors.</p></td></tr><tr><td><pre>(exn bounds)</pre><p>Signaled on errors caused by accessing non-existent elements of a collection.</p></td></tr><tr><td><pre>(exn runtime)</pre><p>Signaled on low-level runtime-system error-situations.</p></td></tr><tr><td><pre>(exn runtime limit)</pre><p>Signaled when an internal limit is exceeded (like running out of memory).</p></td></tr><tr><td><pre>(exn match)</pre><p>Signaled on errors raised by failed matches (see the section on <tt>match</tt>).</p></td></tr><tr><td><pre>(exn syntax)</pre><p>Signaled on syntax errors.</p></td></tr><tr><td><pre>(exn breakpoint)</pre><p>Signaled when a breakpoint is reached.</p></td></tr></table><p>Notes:</p><ul><li>All error-exceptions (of the kind <tt>exn</tt>) are non-continuable.</li><li>Error-exceptions of the <tt>exn</tt> kind have additional <tt>arguments</tt> and <tt>location</tt> properties that contain the arguments passed to the exception-handler and the name of the procedure where the error occurred (if available).</li><li>When the <tt>posix</tt> unit is available and used, then a user-interrupt (<tt>signal/int</tt>) signals an exception of the kind <tt>user-interrupt</tt>.</li><li>the procedure <tt>condition-property-accessor</tt> accepts an optional third argument. If the condition does not have a value for the desired property and if the optional argument is given, no error is signaled and the accessor returns the third argument.</li><li>In composite conditions all properties are currently collected in a single property-list, so in the case that to conditions have the same named property, only one will be visible.</li></ul><a name="environment-information-and-system-interface"></a><h2>Environment information and system interface</h2><a name="argv"></a><h3>argv</h3><pre>[procedure] (argv)</pre><p>Return a list of all supplied command-line arguments. The first item in the list is a string containing the name of the executing program. The other items are the arguments passed to the application. This list is freshly created on every invocation of <tt>(argv)</tt>. It depends on the host-shell whether arguments are expanded ('globbed') or not.</p><a name="exit"></a><h3>exit</h3><pre>[procedure] (exit [CODE])</pre><p>Exit the running process and return exit-code, which defaults to 0 (Invokes <tt>exit-handler</tt>).</p><p>Note that pending <tt>dynamic-wind</tt> thunks are <em>not</em> invoked when exiting your program in this way.</p><a name="build-platform"></a><h3>build-platform</h3><pre>[procedure] (build-platform)</pre><p>Returns a symbol specifying the toolset which has been used for building the executing system, which is one of the following:</p><pre>cygwinmingw32gnu
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -