User:Jarnaldich/sandbox

This page describes and demonstrates features of the Racket programming language. Note that Racket's greatest strength is in making it easy to define and use language dialects, DSLs, and completely new languages, and therefore examples can be in different languages that are all implemented in Racket. Please refer to the main article for more information.

Racket is also highly flexible, even without the use of dialects: it can function as a fully-capable scripting language, capable of running both with and without windows-native GUI, and capable of tasks from web server creation to graphics.

Garbage Collection, Tail Calls, and Space Safety
Racket can use three different garbage collectors:


 * Originally, the conservative Boehm garbage collector has been used. However, conservative collection is impractical for long-running processes such as the web server—such processes tend to slowly leak memory.  In addition, there are pathological cases where a conservative collector leaks memory fast enough to make certain programs impossible to run.  For example, when traversing an infinite list, a single conservative mistake of retaining a pointer leads to keeping the complete list in memory, quickly overflowing available memory.  This collector is often referred to "CGC" in the Racket community.


 * SenoraGC is an alternative conservative garbage collector that is intended mainly for debugging and memory tracing.


 * The moving memory manager (aka "3m") is a precise garbage collector, and it has been Racket's default collector since 2007. This collector is a generational one, and it supports memory accounting via custodians (see below).  The collector is implemented as a C source transformer that is itself written in Racket.  The build process is therefore using the conservative collector for bootstrapping.

Like all implementations in the Scheme family, Racket implements full tail call elimination. Racket takes this further: the language is made fully safe-for-space, via live variable analysis. This complements the precise garbage collector, and in some cases like in the implementation of Lazy Racket, the two features are crucial for proper execution. This is in addition to additional compiler optimizations such as lambda lifting and to its just-in-time compilation.

System Interface and Scripting
Racket's system interface includes asynchronous non-blocking I/O, green threads, synchronization channels, semaphores, sub-processes, and TCP sockets.

The following program starts an "echo server" on port 12345.

The combination of dynamic compilation and a rich system interface makes Racket a capable scripting language, similar to Perl or Python.

The following example demonstrates walking a directory tree, starting at the current directory. It uses the  function to construct a sequence that walks the tree. The  form binds   to each path in the sequence, and   tests these paths against the given regexp pattern.

The next example uses a hash table to record previously seen lines and print only unique ones.

Both of these programs can be run in DrRacket, or on the command line, via the  executable. Racket ignores an initial shebang line, making it easy to turn such programs to executable scripts. The following script demonstrates this. It also provides a library for Command-line argument parsing: The script is a grep-like utility, expecting three command-line arguments: a base directory, a Filename extension, and a (perl-compatible) regular expression. It scans the base directory for files with the given suffix, and print lines matching the regexp pattern.

Resource Management and Sandboxing
Racket features the concept of a "custodian": a kind of value that acts as a resource manager. This is often used in network servers, where each connection is dealt with in a new custodian, making it easy to "clean-up" all resources that might have been left open by the handler (e.g., open ports). The following extends the "echo server" example with such a custodian use:

Custodians, combined with the memory accounting feature of the 3m garbage collector, and a number of additional runtime parameters that control additional aspects of the runtime, make it possible to create completely safe sandboxed execution contexts. The  is providing this kind of functionality in a simple way. The following example creates a "REPL server" on the specified port; connecting to this port will look like a plain Racket REPL, except that the evaluation is subject to the various protection aspects of the sandbox. For example, it is not possible to access the filesystem from this REPL, create network connection, run subprocesses, or use too much time or memory. (In fact, this REPL is safe enough to be given out publicly.)

Web and Network Programming
The next example implements a web server using the  language. Each time a connection is made to the server, the  function is called to get the HTML to send back to the client.

Racket also includes the functions you would need to write scrapers and robots. As an example, the following function would list the Google results for a search string.

The library also includes support for protocols other than http:

Graphics
Graphic capabilities come in several different flavors that are intended for different audiences. The  library provides easy-to-use functions for constructing images, and DrRacket can display an image result just as it can display a number result. This library is mainly used by students in HtDP-based courses. In this case, a  function is defined and called (at the same time) to generate a Sierpinski triangle of depth 8.

The next program uses the  library to draw plots of functions. Note that plots are actual value, which DrRacket shows in graphical form.

GUI Programming
Racket implements a portable GUI layer which the libraries mentioned above build on. It is implemented via the native Windows API, via Cocoa on Mac OS X, and via Gtk on Linux and others. The Racket API is a class-based toolkit, somewhat related to WxWidgets which was used originally.

The following simple guessing game demonstrates coding with the GUI toolkit. The  class implements a top-level window, and   obviously implements a button. The  function defined here produces a function that is used for the button's callback action.

The GUI can be hand-coded in this way or with the help of a Designer program installable from PLaneT, the Racket distributed package system.

Foreign Function Interface
Racket features a foreign function interface that is based on libffi. The interface allows writing unsafe low-level C-like code, that can allocate memory, dereference pointers, call out to functions in shared libraries, and send out callbacks to Racket functions (using libffi closures). The core implementation is a thin layer atop libffi (written in C), and the full interface is then implemented via Racket code. The interface uses macros extensively, resulting in a convenient Racket-based interface description language. The language has a number of convenient features, such as uniform representation for higher-order functions (avoiding the pitfalls when callbacks and callouts are different), struct definitions that are similar to plain Racket structs, and custom function types that can represent input and output pointers, implicit arguments (e.g., an argument that provides the number of elements in a vector that is passed as another argument).

The FFI can be used in a number of different ways: from writing a complete glue layer for a library (as done for Racket's OpenGL binding), to quickly pulling out a single foreign function. An example of the latter approach:

Language Extensions
Racket's most notable feature is its ability to build new domain-specific and general-purpose languages. This is the result of combining a number of important features: The module system plays an important role in combining these features, and making it possible to write code that spans across a number of modules, where each can be written in a different language.
 * a flexible module system that is used for linking code and for namespace management,
 * an extensive macro system—essentially a compiler-API—that can create new syntactic forms,
 * a rich runtime system, providing features that language implementors can use, like (composable, delimited) continuations, resource management, etc,
 * a way to specify (and implement) parsers for new language syntaxes.

Such languages are used extensively in the Racket distribution and in user libraries. In fact, creating a new language is so easy that there are some languages that have less than a handful of uses.

Racket comes with a number of useful languages, some are very different from Racket's default language.

Scribble
Scribble, Racket's documentation system, comes in the form of a number of languages that are used to write prose. It is used for Racket's extensive documentation, as well as writing books and articles. Actually, rather than a single "scribble" language, it is a family of (very similar) dialects, each for a different purpose.

To run the following example, copy it into DrRacket and click one of the two scribble rendering buttons that will appear (PDF rendering requires pdfTeX). Alternatively, use the  executable on the file.

The most striking feature of the Scribble languages is their use of a new syntax, which is designed specifically for textually-rich code. The syntax allows free-form text, string interpolation, customizable quotations, and is useful in other applications such as preprocessing text, generating text, and HTML template systems. Note that the syntax extends plain S-expressions, and is essentially implemented as an alternative input for such expressions.

Typed Racket
Typed Racket is a statically typed variant of Racket. The type system that it implements is unique in that the motivation in developing it was accommodating as much idiomatic Racket code as possible—as a result, it includes subtypes, unions, and much more. Another goal of Typed Racket is to allow migration of parts of a program into the typed language, so it accommodates calling typed code from untyped code and vice versa, generating dynamic contracts to enforce type invariants. This is considered a desirable feature of an application's lifetime stages, as it matures from "a script" to "an application", where static typing helps in maintenance of a large body of code.

Lazy Racket
The  language is a language with lazy evaluation semantics, similar to Haskell. In the following example,  is an infinite list whose 1000th element will not be computed until its value is needed for the printout.

Logic Programming
Racket comes with two logic programming languages: Racklog, a Prolog-like language, and a Datalog implementation. Unlike the Scribble syntax, these languages use a completely new syntax rather than an extension of S-expressions. If you use it in DrRacket, you'll see that it provides proper highlighting, the usual host of tools check syntax, and a Prolog/Datalog REPL.

Educational Tools
The PLT group which develops Racket has traditionally been involved in education at all levels. One of the earliest research ideas that the group promoted is the use of language levels, which restrict new students while providing them with helpful error messages that fit the student's level of knowledge. This approach is heavily used in How to Design Programs, the textbook that several PLT developers have authored, as well as in the ProgramByDesign project. The following program uses the —the "beginning student language". It uses the  library for creating pictures in the teaching languages, and the   library for interactive animations.

Algol
Racket comes with a complete implementation of the ALGOL 60 language. This language is, however, not implemented via  yet. Instead, it must be used inside DrRacket after choosing the "Algol 60" language from the language selection dialog.

Additional Languages
Finally, the following example is an *implementation* of a new language:

This language:
 * provides everything from the  language, so it is a somewhat similar variant,
 * except for two special "hook macros" that implement unbound variable lookup and function calls, instead of these, new forms are provided to
 * implicitly quote all unknown variables
 * allow hash tables to be used as functions, where the arguments are used for hash-table lookup.

If this code is stored in a  file, you can use it as follows:

References and footnotes

 * ''As of Aug 18, 2011 some of the content of this article (contained in examples and descriptive passages) consisted of text from the Racket home page, published with a "copyleft" notice.

Category:Articles with example Racket code