1 | [[tags: manual]] |
---|
2 | |
---|
3 | == Getting started |
---|
4 | |
---|
5 | CHICKEN is a compiler that translates Scheme source files into |
---|
6 | C, which in turn can be fed to a C compiler to generate a |
---|
7 | standalone executable. An interpreter is also available and can be |
---|
8 | used as a scripting environment or for testing programs before |
---|
9 | compilation. |
---|
10 | |
---|
11 | This chapter is designed to get you started with CHICKEN programming, |
---|
12 | describing what it is and what it will do for you, and covering basic |
---|
13 | use of the system. With almost everything discussed here, there is |
---|
14 | more to the story, which the remainder of the manual reveals. Here, we |
---|
15 | only cover enough to get you started. Nonetheless, someone who knows |
---|
16 | Scheme already should be able to use this chapter as the basis for |
---|
17 | writing and running small CHICKEN programs. |
---|
18 | |
---|
19 | === Scheme |
---|
20 | |
---|
21 | Scheme is a member of the Lisp family of languages, of which Common |
---|
22 | Lisp and Emacs Lisp are the other two widely-known members. As with |
---|
23 | Lisp dialects, Scheme features |
---|
24 | |
---|
25 | * a wide variety of programming paradigms, including imperative, functional, and object-oriented |
---|
26 | * a very simple syntax, based upon nested parenthesization |
---|
27 | * the ability to extend the language in meaningful and useful ways |
---|
28 | |
---|
29 | In contrast to Common Lisp, Scheme is very minimal, and tries to |
---|
30 | include only those features absolutely necessary in programming. In |
---|
31 | contrast to Emacs Lisp, Scheme is not anchored into any one program |
---|
32 | (Emacs), and has a somewhat more modern language design. |
---|
33 | |
---|
34 | Scheme is defined in a document called ''The Revised^5 Report on the |
---|
35 | Algorithmic Language Scheme'', or ''R5RS'' for short. (Yes, it really |
---|
36 | has been revised five times, so an expanded version of its name would |
---|
37 | be ''The Revised Revised Revised Revised Revised Report''.) A newer |
---|
38 | report, ''R6RS'', was |
---|
39 | released in 2007, but this report has attracted considerable |
---|
40 | controversy, and not all Scheme implementations will be made compliant |
---|
41 | with it. CHICKEN essentially complies with R5RS. |
---|
42 | |
---|
43 | Even though Scheme is consciously minimalist, it is recognized that a |
---|
44 | language must be more than a minimal core in order to be |
---|
45 | useful. Accordingly, the Scheme community uses a process known as |
---|
46 | `Scheme Requests For Implementation' (SRFI, pronounced `SUR-fee') to |
---|
47 | define new language features. A typical Scheme system therefore |
---|
48 | complies with one of the Scheme reports plus some or all of the |
---|
49 | accepted SRFIs. |
---|
50 | |
---|
51 | A good starting point for Scheme knowledge is |
---|
52 | [[http://www.schemers.org]]. There you will find the defining reports, |
---|
53 | FAQs, lists of useful books and other resources, and the SRFIs. |
---|
54 | |
---|
55 | The CHICKEN community is at present developing tutorials for |
---|
56 | programmers who are new to Scheme but experienced with Python, Ruby, |
---|
57 | or other languages. These can be found on the CHICKEN wiki. |
---|
58 | |
---|
59 | === CHICKEN |
---|
60 | |
---|
61 | CHICKEN is an implementation of Scheme that has many advantages. |
---|
62 | |
---|
63 | CHICKEN Scheme combines an optimising compiler with a reasonably fast |
---|
64 | interpreter. It supports almost all of R5RS and the important SRFIs. |
---|
65 | The compiler generates portable C code that supports tail recursion, |
---|
66 | first-class continuations, and lightweight threads, and the interface to |
---|
67 | and from C libraries is flexible, efficient, and easy to use. There are |
---|
68 | hundreds of contributed CHICKEN libraries that make the programmer's |
---|
69 | task easier. The interpreter allows interactive use, fast prototyping, |
---|
70 | debugging, and scripting. The active and helpful CHICKEN community |
---|
71 | fixes bugs and provides support. Extensive documentation is supplied. |
---|
72 | |
---|
73 | CHICKEN was developed by Felix L. Winkelmann over the period from 2000 |
---|
74 | through 2007. In early 2008, Felix |
---|
75 | asked the community to take over the responsibility of developing and |
---|
76 | maintaining the system, though he still takes a strong interest in it, |
---|
77 | and participates actively. |
---|
78 | |
---|
79 | CHICKEN includes |
---|
80 | |
---|
81 | * a Scheme interpreter that supports almost all of R5RS Scheme, with |
---|
82 | only a few relatively minor omissions, and with many extensions |
---|
83 | * a compatible compiler whose target is C, thus making porting to new |
---|
84 | machines and architectures relatively straightforward |
---|
85 | ** the C support allows Scheme code to include `embedded' C code, |
---|
86 | thus making it relatively easy to invoke host OS or library |
---|
87 | functions |
---|
88 | * a framework for language extensions, library modules that broaden |
---|
89 | the functionality of the system |
---|
90 | |
---|
91 | This package is distributed under the '''BSD license''' and as such is free |
---|
92 | to use and modify. |
---|
93 | |
---|
94 | Scheme cognoscenti will appreciate the method of compilation and the |
---|
95 | design of the runtime-system, which follow closely Henry Baker's |
---|
96 | [[http://home.pipeline.com/~hbaker1/CheneyMTA.html|CONS Should Not |
---|
97 | CONS Its Arguments, Part II: Cheney on the M.T.A.]] paper and expose a |
---|
98 | number of interesting properties. |
---|
99 | |
---|
100 | * Consing (creation of data on the heap) is relatively inexpensive, |
---|
101 | because a generational garbage collection scheme is used, in which |
---|
102 | short-lived data structures are reclaimed extremely quickly. |
---|
103 | |
---|
104 | * Moreover, {{call-with-current-continuation}} is practically for free |
---|
105 | and CHICKEN does not suffer under any performance penalties if |
---|
106 | first-class continuations are used in complex ways. |
---|
107 | |
---|
108 | The generated C code is fully tail-recursive. |
---|
109 | |
---|
110 | Some of the features supported by CHICKEN: |
---|
111 | |
---|
112 | * SRFIs 0, 1, 2, 4, 6, 8-18, 23, 26, 28, 30, 31, 39, 46, 55, 61, 62, 69, 85, 88 and 98. |
---|
113 | * Lightweight threads based on first-class continuations |
---|
114 | * Record structures |
---|
115 | * Extended comment- and string-literal syntaxes |
---|
116 | * Libraries for regular expressions, string handling |
---|
117 | * UNIX system calls and extended data structures |
---|
118 | * Create interpreted or compiled shell scripts written in Scheme for |
---|
119 | UNIX or Windows |
---|
120 | * Compiled C files can be easily distributed |
---|
121 | * Allows the creation of fully self-contained statically linked executables |
---|
122 | * On systems that support it, compiled code can be loaded dynamically |
---|
123 | * Built-in support for cross-compilation and deployment |
---|
124 | |
---|
125 | CHICKEN has been used in many environments ranging from embedded |
---|
126 | systems through desktop machines to large-scale server deployments. |
---|
127 | The number of language extensions, or '''eggs''', is constantly growing. |
---|
128 | |
---|
129 | * extended language features |
---|
130 | * development tools, such as documentation generators, debugging, and |
---|
131 | automated testing libraries |
---|
132 | * interfaces to other languages such as Java, Python, and Objective-C |
---|
133 | * interfaces to database systems, GUIs, and other large-scale |
---|
134 | libraries, |
---|
135 | * network applications, such as servers and clients for ftp, |
---|
136 | smtp/pop3, irc, and http |
---|
137 | * web servers and related tools, including URL parsing, HTML |
---|
138 | generation, AJAX, and HTTP session management |
---|
139 | * data formats, including XML, JSON, and Unicode support |
---|
140 | |
---|
141 | CHICKEN is supported by SWIG (Simplified Wrapper and Interface |
---|
142 | Generator), a tool that produces quick-and-dirty interface modules |
---|
143 | for C libraries ([[http://www.swig.org]]). |
---|
144 | |
---|
145 | This chapter provides you with an overview of the entire system, with |
---|
146 | enough information to get started writing and running small Scheme |
---|
147 | programs. |
---|
148 | |
---|
149 | === CHICKEN repositories, websites, and community |
---|
150 | |
---|
151 | The master CHICKEN website is |
---|
152 | [[http://www.call-with-current-continuation.org]]. Here you can find |
---|
153 | basic information about CHICKEN, downloads, and pointers to other key |
---|
154 | resources. |
---|
155 | |
---|
156 | The CHICKEN wiki ([[http://wiki.call-cc.org]]) contains the most |
---|
157 | current version of the User's manual, along with various tutorials and |
---|
158 | other useful documents. The list of eggs is at |
---|
159 | [[http://wiki.call-cc.org/egg-index]]. |
---|
160 | |
---|
161 | A very useful search facility for questions about CHICKEN is found at |
---|
162 | [[http://api.call-cc.org]]. The CHICKEN issue tracker is at |
---|
163 | [[http://bugs.call-cc.org]]. |
---|
164 | |
---|
165 | The CHICKEN community has two major mailing lists. If you are a |
---|
166 | CHICKEN user, {{chicken-users}} |
---|
167 | ([[http://lists.nongnu.org/mailman/listinfo/chicken-users]]) will be |
---|
168 | of interest. The crew working on the CHICKEN system itself uses the |
---|
169 | very low-volume {{chicken-hackers}} list |
---|
170 | ([[http://lists.nongnu.org/mailman/listinfo/chicken-hackers]]) for |
---|
171 | communication. |
---|
172 | |
---|
173 | === Installing CHICKEN |
---|
174 | |
---|
175 | CHICKEN is available in binary form for Windows and Linux/x86 |
---|
176 | systems, and in source form for all other platforms. Refer to the |
---|
177 | {{README}} file in the distribution for instructions on installing it |
---|
178 | on your system. |
---|
179 | |
---|
180 | Because it compiles to C, CHICKEN requires that a C compiler be |
---|
181 | installed on your system. (If you're not writing embedded C code, you |
---|
182 | can pretty much ignore the C compiler once you have installed it.) |
---|
183 | |
---|
184 | * On a Linux system, the GNU Compiler Collection ({{gcc}}) should be |
---|
185 | installed as part of the basic operating system, or should be |
---|
186 | available through the package management system (e.g., APT, |
---|
187 | Synaptic, RPM, or Yum, depending upon your Linux distribution). |
---|
188 | * On Macintosh OS X, you will need the XCode tools, which are shipped |
---|
189 | on the OS X DVD with recent versions of the operating system. |
---|
190 | * On Windows, you have three choices. |
---|
191 | ** Cygwin ([[http://sources.redhat.com/cygwin]]) provides a relatively |
---|
192 | full-featured Unix environment for Windows. CHICKEN works |
---|
193 | substantially the same in Cygwin and Unix. |
---|
194 | ** The GNU Compiler Collection has been ported to Windows, in the |
---|
195 | MinGW system ([[http://mingw.sourceforge.net]]). Unlike Cygwin, |
---|
196 | executables produced with MinGW do not need the Cygwin DLLs in order |
---|
197 | to run. MSys is a companion package to MinGW; it provides a minimum |
---|
198 | Unix-style development/build environment, again ported from free |
---|
199 | software. |
---|
200 | *** You can build CHICKEN either with MinGW alone or with MinGW plus |
---|
201 | MSYS. Both approaches produce a CHICKEN built against the mingw headers |
---|
202 | and import libraries. |
---|
203 | The only difference is the environment where you actually run make. |
---|
204 | {{Makefile.mingw}} is can be used in {{cmd.exe}} with the version of make |
---|
205 | that comes with mingw. {{Makefile.mingw-msys}} |
---|
206 | uses unix commands such as {{cp}} and {{rm}}. The end product is the |
---|
207 | same. |
---|
208 | |
---|
209 | Refer to the {{README}} file for the version you're installing for |
---|
210 | more information on the installation process. |
---|
211 | |
---|
212 | Alternatively, third party packages in binary format are available. |
---|
213 | Se [[http://wiki.call-cc.org/platforms]] for information about how to |
---|
214 | obtain them. |
---|
215 | |
---|
216 | === Development environments |
---|
217 | |
---|
218 | The simplest development environment is a text editor and terminal |
---|
219 | window (Windows: Command Prompt, OSX: Terminal, Linux/Unix: xterm) for |
---|
220 | using the interpreter and/or calling the compiler. If you |
---|
221 | [[/egg/readline|install the {{readline}} egg]], you |
---|
222 | have all the benefits of command history in the interpreter, Emacs or |
---|
223 | vi-compatible line editing, and customization. |
---|
224 | |
---|
225 | You will need a text editor that knows Scheme; it's just too painful |
---|
226 | with editors that don't do parenthesis matching and proper |
---|
227 | indentation. Some editors allow you to execute Scheme code directly in |
---|
228 | the editor. This makes programming very interactive: you can type in a |
---|
229 | function and then try it right away. This feature is very highly |
---|
230 | recommended. |
---|
231 | |
---|
232 | As programmers have very specific tastes about editors, the editors |
---|
233 | listed here are shown in alphabetic order. We aren't about to tell you |
---|
234 | which editor to use, and there may be editors not shown here that |
---|
235 | might satisfy your needs. We would be very interested in reports of |
---|
236 | other editors that have been used with CHICKEN, especially those that |
---|
237 | support interactive evaluation of forms during editing. Pointers to |
---|
238 | these (and to any editor customization files appropriate) should be |
---|
239 | put on the CHICKEN wiki, and will likely be added to future editions |
---|
240 | of this manual. (We have had a request for editors that support |
---|
241 | proportional fonts, in particular.) |
---|
242 | |
---|
243 | * Emacs ([[http://www.gnu.org/software/emacs]]) is an |
---|
244 | extensible, customizable, self-documenting editor available for |
---|
245 | Linux/Unix, Macintosh, and Windows systems; See |
---|
246 | [[/emacs]] for more information about the available options. |
---|
247 | |
---|
248 | * Epsilon ([[http://www.lugaru.com]]) is a commercial (proprietary) text |
---|
249 | editor whose design was inspired by Emacs. Although Scheme support |
---|
250 | isn't provided, a Lisp mode is available on Lugaru's FTP site, and |
---|
251 | could with some work be made to duplicate the Emacs support. |
---|
252 | |
---|
253 | * SciTE ([[http://scintilla.sourceforge.net/SciTE.html]]), |
---|
254 | unlike Emacs or Vim, follows typical graphical UI design conventions |
---|
255 | and control-key mappings, and for simple tasks is as familiar and |
---|
256 | easy to use as Notepad, KEdit, TeachText etc. However it has many |
---|
257 | programming features such as multiple open files, syntax |
---|
258 | highlighting for a large number of languages (including Lisps), |
---|
259 | matching of brackets, ability to fold sections of code based on the |
---|
260 | matched brackets, column selections, comment/uncomment, and the |
---|
261 | ability to run commands in the same directory as the current file |
---|
262 | (such as make, grep, etc.) SciTE is written with the GTK toolkit |
---|
263 | and is portable to any GTK platform, including Windows, Linux and |
---|
264 | MacOS. It uses the Scintilla text-editing component, which lends |
---|
265 | itself well to embedding within other IDEs and graphical toolkits. |
---|
266 | It does not have any other Scheme-specific features, but being |
---|
267 | open-source and modular, features like auto-formatting of |
---|
268 | S-expressions could be added. The syntax highlighting can be |
---|
269 | configured to use different fonts for different types of syntax, |
---|
270 | including proportional fonts. |
---|
271 | |
---|
272 | * Vim ([[http://www.vim.org]]) is a highly configurable text |
---|
273 | editor built to enable efficient and fast text editing. It is an |
---|
274 | improved version of the vi editor distributed with most UNIX |
---|
275 | systems. Vim comes with generic Lisp (and therefore Scheme) editing |
---|
276 | capabilities out of the |
---|
277 | box. A few tips on using Vim with CHICKEN can be found at |
---|
278 | [[http://cybertiggyr.com/gene/15-vim/]]. |
---|
279 | |
---|
280 | In the rest of this chapter, we'll assume that you are using an editor |
---|
281 | of your choice and a regular terminal window for executing your |
---|
282 | CHICKEN code. |
---|
283 | |
---|
284 | === The Read-Eval-Print loop |
---|
285 | |
---|
286 | To invoke the CHICKEN interpreter, you use the {{csi}} command. |
---|
287 | |
---|
288 | |
---|
289 | $ csi |
---|
290 | |
---|
291 | CHICKEN |
---|
292 | (c)2008-2010 The Chicken Team |
---|
293 | (c)2000-2007 Felix L. Winkelmann |
---|
294 | Version 4.6.0 |
---|
295 | macosx-unix-gnu-x86 [ manyargs dload ptables ] |
---|
296 | |
---|
297 | #;1> |
---|
298 | |
---|
299 | This brings up a brief banner, and then the prompt. You can use this |
---|
300 | pretty much like any other Scheme system, e.g., |
---|
301 | |
---|
302 | #;1> (define (twice f) (lambda (x) (f (f x)))) |
---|
303 | #;2> ((twice (lambda (n) (* n 10))) 3) |
---|
304 | 300 |
---|
305 | |
---|
306 | Suppose we have already created a file {{fact.scm}} containing a |
---|
307 | function definition. |
---|
308 | |
---|
309 | (define (fact n) |
---|
310 | (if (= n 0) |
---|
311 | 1 |
---|
312 | (* n (fact (- n 1))))) |
---|
313 | |
---|
314 | We can now load this file and try out the function. |
---|
315 | |
---|
316 | #;3> (load "fact.scm") |
---|
317 | ; loading fact.scm ... |
---|
318 | #;4> (fact 3) |
---|
319 | 6 |
---|
320 | |
---|
321 | The '''read-eval-print loop''' ('''REPL''') is the component of the |
---|
322 | Scheme system that ''reads'' a Scheme expression, ''eval''uates it, |
---|
323 | and ''prints'' out the result. The REPL's prompt can be customized |
---|
324 | (see the [[Using the interpreter]]) |
---|
325 | but the default prompt, showing the number of the form, is quite |
---|
326 | convenient. |
---|
327 | |
---|
328 | The REPL also supports debugging commands: |
---|
329 | input lines beginning with a {{,}} (comma) are treated as special |
---|
330 | commands. (See the [[Using the interpreter#Toplevel commands|full list]].) |
---|
331 | |
---|
332 | |
---|
333 | ==== Scripts |
---|
334 | |
---|
335 | You can use the interpreter to run a Scheme program from the command |
---|
336 | line. For the following example we create a program that does a quick |
---|
337 | search-and-replace on an input file; the arguments are a regular |
---|
338 | expression and a replacement string. First create a file to hold the "data" called ''quickrep.dat'' with your favorite editor holding these lines: |
---|
339 | |
---|
340 | xyzabcghi |
---|
341 | abxawxcgh |
---|
342 | foonly |
---|
343 | |
---|
344 | Next create the scheme code in a file called ''quickrep.scm'' with the |
---|
345 | following little program: |
---|
346 | |
---|
347 | <enscript highlight=scheme> |
---|
348 | |
---|
349 | (use irregex) ; irregex, the regular expression library, is one of the |
---|
350 | ; libraries included with CHICKEN. |
---|
351 | |
---|
352 | (define (process-line line re rplc) |
---|
353 | (irregex-replace/all re line rplc)) |
---|
354 | |
---|
355 | (define (quickrep re rplc) |
---|
356 | (let ((line (read-line))) |
---|
357 | (if (not (eof-object? line)) |
---|
358 | (begin |
---|
359 | (display (process-line line re rplc)) |
---|
360 | (newline) |
---|
361 | (quickrep re rplc))))) |
---|
362 | |
---|
363 | ;;; Does a lousy job of error checking! |
---|
364 | (define (main args) |
---|
365 | (quickrep (irregex (car args)) (cadr args))) |
---|
366 | </enscript> |
---|
367 | |
---|
368 | |
---|
369 | To run it enter this in your shell: |
---|
370 | |
---|
371 | $ csi -ss quickrep.scm <quickrep.dat 'a.*c' A |
---|
372 | xyzAghi |
---|
373 | Agh |
---|
374 | foonly |
---|
375 | |
---|
376 | The {{-ss}} option sets several options that work smoothly together to |
---|
377 | execute a script. You can make the command directly executable from |
---|
378 | the shell by inserting a `[[Using the interpreter#Writing Scheme scripts|shebang line]]' at the beginning of the |
---|
379 | program. |
---|
380 | |
---|
381 | The {{-ss}} option arranges to call a procedure named {{main}}, with |
---|
382 | the command line arguments, packed in a list, as its arguments. (There |
---|
383 | are a number of ways this program could be made more idiomatic CHICKEN |
---|
384 | Scheme, see the rest of the manual for details.) |
---|
385 | |
---|
386 | === The compiler |
---|
387 | |
---|
388 | There are several reasons you might want to compile your code. |
---|
389 | |
---|
390 | * Compiled code executes substantially faster than interpreted |
---|
391 | code. |
---|
392 | * You might want to deploy an application onto machines where the |
---|
393 | users aren't expected to have CHICKEN installed: compiled |
---|
394 | applications can be self-contained. |
---|
395 | |
---|
396 | The CHICKEN compiler is provided as the command {{chicken}}, but in |
---|
397 | almost all cases, you will want to use the {{csc}} command |
---|
398 | instead. {{csc}} is a convenient driver that automates compiling |
---|
399 | Scheme programs into C, compiling C code into object code, and linking |
---|
400 | the results into an executable file. (Note: in a Windows environment |
---|
401 | with Visual Studio, you may find that {{csc}} refers to Microsoft's |
---|
402 | C# compiler. There are a number of ways of sorting this out, of which |
---|
403 | the simplest is to rename one of the two tools, and/or to |
---|
404 | organize your {{PATH}} according to the task at hand.) |
---|
405 | |
---|
406 | Compiled code can be intermixed with interpreted code on systems that |
---|
407 | support dynamic loading, which includes modern versions of *BSD, |
---|
408 | Linux, Mac OS X, Solaris, and Windows. |
---|
409 | |
---|
410 | We can compile our factorial function, producing a file named |
---|
411 | {{fact.so}} (`shared object' in Linux-ese, the same file type is used |
---|
412 | in OS X and Windows, rather than {{dylib}} or {{dll}}, respectively). |
---|
413 | |
---|
414 | chicken$ csc -dynamic fact.scm |
---|
415 | chicken$ csi -quiet |
---|
416 | #;1> (load "fact.so") |
---|
417 | ; loading fact.so ... |
---|
418 | #;2> (fact 6) |
---|
419 | 720 |
---|
420 | |
---|
421 | On any system, we can just compile a program directly into an |
---|
422 | executable. Here's a program that tells you whether its argument is a |
---|
423 | palindrome. |
---|
424 | |
---|
425 | <enscript highlight=scheme> |
---|
426 | (define (palindrome? x) |
---|
427 | (define (check left right) |
---|
428 | (if (>= left right) |
---|
429 | #t |
---|
430 | (and (char=? (string-ref x left) (string-ref x right)) |
---|
431 | (check (add1 left) (sub1 right))))) |
---|
432 | (check 0 (sub1 (string-length x)))) |
---|
433 | (let ((arg (car (command-line-arguments)))) |
---|
434 | (display |
---|
435 | (string-append arg |
---|
436 | (if (palindrome? arg) |
---|
437 | " is a palindrome\n" |
---|
438 | " isn't a palindrome\n")))) |
---|
439 | </enscript> |
---|
440 | |
---|
441 | We can compile this program using {{csc}}, creating an executable |
---|
442 | named {{palindrome}}. |
---|
443 | |
---|
444 | $ csc -o palindrome palindrome.scm |
---|
445 | $ ./palindrome level |
---|
446 | level is a palindrome |
---|
447 | $ ./palindrome liver |
---|
448 | liver isn't a palindrome |
---|
449 | |
---|
450 | CHICKEN supports separate compilation, using some extensions to |
---|
451 | Scheme. Let's divide our palindrome program into a library module |
---|
452 | ({{pal-proc.scm}}) and a client module ({{pal-user.scm}}). |
---|
453 | |
---|
454 | Here's the external library. We {{declare}} that {{pal-proc}} is a |
---|
455 | `unit', which is the basis of separately-compiled modules in |
---|
456 | CHICKEN. (Units deal with separate compilation, but don't involve |
---|
457 | separated namespaces; namespaced module systems are available as |
---|
458 | eggs.) |
---|
459 | |
---|
460 | <enscript highlight=scheme> |
---|
461 | ;;; Library pal-proc.scm |
---|
462 | (declare (unit pal-proc)) |
---|
463 | (define (palindrome? x) |
---|
464 | (define (check left right) |
---|
465 | (if (>= left right) |
---|
466 | #t |
---|
467 | (and (char=? (string-ref x left) (string-ref x right)) |
---|
468 | (check (add1 left) (sub1 right))))) |
---|
469 | (check 0 (sub1 (string-length x)))) |
---|
470 | </enscript> |
---|
471 | |
---|
472 | Next we have some client code that `uses' this separately-compiled |
---|
473 | module. |
---|
474 | |
---|
475 | <enscript highlight=scheme> |
---|
476 | ;;; Client pal-user.scm |
---|
477 | (declare (uses pal-proc)) |
---|
478 | (let ((arg (car (command-line-arguments)))) |
---|
479 | (display |
---|
480 | (string-append arg |
---|
481 | (if (palindrome? arg) |
---|
482 | " is a palindrome\n" |
---|
483 | " isn't a palindrome\n")))) |
---|
484 | </enscript> |
---|
485 | |
---|
486 | Now we can compile and link everything together. (We show the compile |
---|
487 | and link operations separately, but they can of course be combined |
---|
488 | into one command.) |
---|
489 | |
---|
490 | $ csc -c pal-proc.scm |
---|
491 | $ csc -c pal-user.scm |
---|
492 | $ csc -o pal-separate pal-proc.o pal-user.o |
---|
493 | $ ./pal-separate level |
---|
494 | level is a palindrome |
---|
495 | |
---|
496 | === Installing an egg |
---|
497 | |
---|
498 | Installing eggs is quite straightforward on systems that support |
---|
499 | dynamic loading (again, that would include *BSD, Linux, Mac OS X, |
---|
500 | Solaris, and Windows). The command {{chicken-install}} will fetch an |
---|
501 | egg from the master CHICKEN repository, and install it on your local |
---|
502 | system. |
---|
503 | |
---|
504 | In this example, we install the {{uri-common}} egg, for parsing |
---|
505 | Uniform Resource Identifiers. The installation produces a lot of |
---|
506 | output, which we have edited for space. |
---|
507 | |
---|
508 | $ chicken-install uri-common |
---|
509 | |
---|
510 | retrieving ... |
---|
511 | resolving alias `kitten-technologies' to: http://chicken.kitten-technologies.co.uk/henrietta.cgi |
---|
512 | connecting to host "chicken.kitten-technologies.co.uk", port 80 ... |
---|
513 | requesting "/henrietta.cgi?name=uri-common&mode=default" ... |
---|
514 | reading response ... |
---|
515 | [...] |
---|
516 | /usr/bin/csc -feature compiling-extension -setup-mode -s -O2 uri-common.scm -j uri-common |
---|
517 | /usr/bin/csc -feature compiling-extension -setup-mode -s -O2 uri-common.import.scm |
---|
518 | cp -r uri-common.so /usr/lib/chicken/5/uri-common.so |
---|
519 | chmod a+r /usr/lib/chicken/5/uri-common.so |
---|
520 | cp -r uri-common.import.so /usr/lib/chicken/5/uri-common.import.so |
---|
521 | chmod a+r /usr/lib/chicken/5/uri-common.import.so |
---|
522 | chmod a+r /usr/lib/chicken/5/uri-common.setup-info |
---|
523 | |
---|
524 | {{chicken-install}} connects to a mirror of the egg repository and |
---|
525 | retrieves the egg contents. If the egg has any uninstalled |
---|
526 | dependencies, it recursively installs them. Then it builds the egg |
---|
527 | code and installs the resulting extension into the |
---|
528 | local CHICKEN repository. |
---|
529 | |
---|
530 | Now we can use our new egg. |
---|
531 | |
---|
532 | #;1> (use uri-common) |
---|
533 | ; loading /usr/lib/chicken/5/uri-common.import.so ... |
---|
534 | ; [... other loaded files omitted for clarity ...] |
---|
535 | |
---|
536 | #;2> (uri-host (uri-reference "http://www.foobar.org/blah")) |
---|
537 | "www.foobar.org" |
---|
538 | |
---|
539 | === Accessing C libraries |
---|
540 | |
---|
541 | Because CHICKEN compiles to C, and because a foreign function |
---|
542 | interface is built into the compiler, interfacing to a C library is |
---|
543 | quite straightforward. This means that nearly any facility available |
---|
544 | on the host system is accessible from CHICKEN, with more or less |
---|
545 | work. |
---|
546 | |
---|
547 | Let's create a simple C library, to demonstrate how this |
---|
548 | works. Here we have a function that will compute and return the '''n'''th |
---|
549 | Fibonacci number. (This isn't a particularly good use of C here, |
---|
550 | because we could write this function just as easily in Scheme, but a |
---|
551 | real example would take far too much space here.) |
---|
552 | |
---|
553 | /* fib.c */ |
---|
554 | int fib(int n) { |
---|
555 | int prev = 0, curr = 1; |
---|
556 | int next; |
---|
557 | int i; |
---|
558 | for (i = 0; i < n; i++) { |
---|
559 | next = prev + curr; |
---|
560 | prev = curr; |
---|
561 | curr = next; |
---|
562 | } |
---|
563 | return curr; |
---|
564 | } |
---|
565 | |
---|
566 | Now we can call this function from CHICKEN. |
---|
567 | |
---|
568 | ;;; fib-user.scm |
---|
569 | #> |
---|
570 | extern int fib(int n); |
---|
571 | <# |
---|
572 | (define xfib (foreign-lambda int "fib" int)) |
---|
573 | (do ((i 0 (+ i 1))) ((> i 10)) |
---|
574 | (printf "~A " (xfib i))) |
---|
575 | (newline) |
---|
576 | |
---|
577 | The syntax {{#>...<#}} allows you to include literal C (typically |
---|
578 | external declarations) in your CHICKEN code. We access {{fib}} by |
---|
579 | defining a {{foreign-lambda}} for it, in this case saying that the |
---|
580 | function takes one integer argument (the {{int}} after the function |
---|
581 | name), and that it returns an integer result (the {{int}} before.) Now we can invoke |
---|
582 | {{xfib}} as though it were an ordinary Scheme function. |
---|
583 | |
---|
584 | $ gcc -c fib.c |
---|
585 | $ csc -o fib-user fib.o fib-user.scm |
---|
586 | $ ./fib-user |
---|
587 | 0 1 1 2 3 5 8 13 21 34 55 |
---|
588 | |
---|
589 | Those who are interfacing to substantial C libraries should consider |
---|
590 | using the [[/egg/bind|bind egg]]. |
---|
591 | |
---|
592 | --- |
---|
593 | |
---|
594 | Back to [[The User's Manual]] |
---|
595 | |
---|
596 | Next: [[Basic mode of operation]] |
---|