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

📄 getting-started.html

📁 Scheme跨平台编译器
💻 HTML
📖 第 1 页 / 共 2 页
字号:
                      " isn't a palindrome\n")))) </pre><p>We can compile this program using <tt>csc</tt>, creating an executable named <tt>palindrome</tt>. </p><pre>$ csc -o palindrome palindrome.scm$ ./palindrome levellevel is a palindrome$ ./palindrome liverliver isn't a palindrome</pre><p>Chicken supports separate compilation, using some extensions to Scheme. Let's divide our palindrome program into a library module (<tt>pal-proc.scm</tt>) and a client module (<tt>pal-user.scm</tt>). </p><p>Here's the external library. We <tt>declare</tt> that <tt>pal-proc</tt> is a `unit', which is the basis of separately-compiled modules in Chicken. (Units deal with separate compilation, but don't involve separated namespaces; namespaced module systems are available as eggs.)</p><pre>;;; Library pal-proc.scm(declare (unit pal-proc))(define (palindrome? x)  (define (check left right)    (if (&gt;= left right)        #t        (and (char=? (string-ref x left) (string-ref x right))             (check (add1 left) (sub1 right)))))  (check 0 (sub1 (string-length x))))</pre><p>Next we have some  client code that `uses' this separately-compiled module.  </p><pre>;;; Client pal-user.scm(declare (uses pal-proc))(let ((arg (car (command-line-arguments))))  (display    (string-append arg                   (if (palindrome? arg)                       " is a palindrome\n"                      " isn't a palindrome\n"))))</pre><p>Now we can compile and link everything together. (We show the compile and link operations separately, but they can of course be combined into one command.) </p><pre>$ csc -c pal-proc.scm$ csc -c pal-user.scm$ csc -o pal-separate pal-proc.o pal-user.o$ ./pal-separate levellevel is a palindrome</pre><a name="installing-an-egg"></a><h2>Installing an egg</h2><p>Installing eggs is quite straightforward on systems that support dynamic loading (again, that would include *BSD, Linux, Mac OS X, Solaris, and Windows).  The command <tt>chicken-setup</tt> will fetch an egg from the master Chicken repository, and install it on your local system.</p><p>In this example, we install the <tt>uri</tt> egg, for parsing Uniform Resource Identifiers. The installation produces a lot of output, which we have edited for space reasons.</p><pre>$ chicken-setup uri</pre><pre>The extension uri does not exist.Do you want to download it ? (yes/no/abort) [yes] yesdownloading uri.egg from (www.call-with-current-continuation.org eggs/3 80)   gzip -d -c ../uri.egg | tar xf -.  /Users/vmanis/local/bin/csc -feature compiling-extension      -s -O2 -d1 uri.scm -o uri.so -check-imports -emit-exports uri.exports... (lots of stuff elided).  rm -fr /Users/vmanis/project/chicken/uri.egg</pre><p>First, <tt>chicken-setup</tt> asks us if we want to download the egg. It then uncompresses the egg, compiles the code, and installs the egg in the local Chicken repository. </p><p>Now we can use our new egg. </p><pre>#;1&gt; (use uri); loading /Users/vmanis/local/lib/chicken/3/uri.so ...; loading /Users/vmanis/local/lib/chicken/3/coerce-support.so ...; loading /Users/vmanis/local/lib/chicken/3/misc-extn-list-support.so ...; loading /Users/vmanis/local/lib/chicken/3/synch-support.so ...; loading /Users/vmanis/local/lib/chicken/3/lookup-table.so ...; loading /Users/vmanis/local/lib/chicken/3/misc-extn-control-support.so ...#;2&gt; (uri-host (uri "<a href="http://www.foobar.org/blah" class="external">http://www.foobar.org/blah</a>"))"www.foobar.org"</pre><a name="accessing-c-libraries"></a><h2>Accessing C libraries</h2><p>Because Chicken compiles to C, and because a foreign function interface is built into the compiler, interfacing to a C library is quite straightforward. This means that nearly any facility available on the host system is accessible from Chicken, with more or less work. </p><p>Let's create a simple C library, to demonstrate how this works. Here we have a function that will compute and return the <strong>n</strong>th Fibonacci number. (This isn't a particularly good use of C here, because we could write this function just as easily in Scheme, but a real example would take far too much space here.) </p><pre>int fib(int n) {  int prev = 0, curr = 1;  int next;   int i;   for (i = 0; i &lt; n; i++) {    next = prev + curr;    prev = curr;    curr = next;   }  return curr;} </pre><p>Now we can call this function from Chicken. </p><pre>#&gt;  extern fib(int n);&lt;# (define xfib (foreign-lambda int "fib" int))(do ((i 0 (+ i 1))) ((&gt; i 10))  (printf "~A " (xfib i)))(newline)</pre><p>The syntax <tt>#&gt;...&lt;#</tt> allows you to include literal C (typically external declarations) in your Chicken code. We access <tt>fib</tt> by defining a <tt>foreign-lambda</tt> for it, in this case saying that the function takes one integer argument (the <tt>int</tt> after the function name), and that it returns an integer result (the <tt>int</tt> before.) Now we can invoke <tt>xfib</tt> as though it were an ordinary Scheme function. </p><pre>$ gcc -c fib.c$ csc -o fib-user fib.o fib-user.scm$ ./fib-user0 1 1 2 3 5 8 13 21 34 55 </pre><p>Those who are interfacing to substantial C libraries should consider using the  easyffi egg, or SWIG. </p><p>Back to <a href="index.html" class="internal">index.html</a> Next: <a href="basic-mode-of-operation.html" class="internal">Basic mode of operation</a></p></body></html>

⌨️ 快捷键说明

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