词条 | Racket features |
释义 |
Racket has been under active development as a vehicle for programming language research since the mid-1990s, and has accumulated many features over the years. This article describes and demonstrates some of these features. Note that one of Racket's main design goals is to accommodate creating new languages, both domain-specific languages and completely new languages.[1] Therefore, some of the following examples are in different languages, but they are all implemented in Racket. Please refer to the main article for more information. The core Racket implementation is highly flexible. Even without using dialects, it can function as a full-featured scripting language, capable of running both with and without windows-native GUI, and capable of tasks from web server creation to graphics. Runtime SupportGarbage Collection, Tail Calls, and Space SafetyRacket can use three different garbage collectors:
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 just-in-time compilation. System Interface and ScriptingRacket'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 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 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 SandboxingRacket 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 Web and Network ProgrammingThe next example implements a web server using the 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: GraphicsGraphic capabilities come in several different flavors that are intended for different audiences. The DrRacket editors can contain images, and DrRacket displays image values just like any other type of value (such as integers or lists). Running the above program, for example, actually displays a Sierpinski triangle, which can be cut and pasted into another program. The Here, the GUI ProgrammingRacket 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 The GUI can be hand-coded in this way or with the help of a GUI designer program available on PLaneT.[2] SlideshowSlide-based presentations can also be developed in Racket using the For example, the following program displays in full-screen a title slide, followed by a slide with some pictures. The Extension packages also exist on PLaneT,[2] for example to include LaTeX elements. Foreign Function InterfaceRacket 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 an expressive Racket-based interface description language. This language has a number of useful 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). By using this interface to access underlying GUI toolkits, Racket implements its own GUI layer completely in Racket.[3] 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 ExtensionsRacket'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. Such languages are used extensively in the Racket distribution and in user libraries. In fact, creating a new language is so straightforward, 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. ScribbleScribble, 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 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 The most striking feature of the Scribble languages is their use of a new syntax, which is designed specifically for textually rich code.[4] 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 implemented as an alternative input for such expressions. Typed RacketTyped 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.[5] 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.[6] 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 RacketThe Logic ProgrammingRacket comes with three logic programming languages: Racklog, a Prolog-like language; a Datalog implementation; and a miniKanren port. Unlike the Scribble syntax, the first two of 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 ToolsThe 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 AlgolRacket comes with a complete implementation of the ALGOL 60 language. Plai and plai-typedAnother supported language is plai which like racket can be typed or untyped. "Modules written in plai export every definition (unlike scheme)." [7] "The Typed PLAI language differs from traditional Racket most importantly by being statically typed. It also gives you some useful new constructs: define-type, type-case, and test." [8] Additional LanguagesFinally, the following example is an implementation of a new language: This language:
If this code is stored in a References1. ^{{cite conference | last1 = Tobin-Hochstadt | first1 = S. | last2 = St-Amour | first2 = V. | last3 = Culpepper | first3 = R. | last4 = Flatt | first4 = M. | last5 = Felleisen | first5 = M. | title = Languages as Libraries | booktitle = Programming Language Design and Implementation | year = 2011 | url = http://www.ccs.neu.edu/scheme/pubs/pldi11-thacff.pdf}} 2. ^1 PLaneT: Racket's centralized package distribution system 3. ^{{cite web | title = Rebuilding Racket's Graphics Layer | accessdate = 2013-07-07 | date = 2010-12-08 | url = http://blog.racket-lang.org/2010/12/racket-version-5.html | deadurl = yes | archiveurl = https://www.webcitation.org/6GpxF6OGj?url=http://blog.racket-lang.org/2010/12/racket-version-5.html | archivedate = 2013-05-23 | df = }} 4. ^{{cite conference | last1 = Barzilay | first1 = E. | title = The Scribble Reader | booktitle = Scheme and Functional Programming | year = 2009 | url = http://www.ccs.neu.edu/racket/pubs/scheme2009-b.pdf}} 5. ^{{cite conference | last1 = Tobin-Hochstadt | first1 = S. | last2 = Felleisen | first2 = M. | title = The Design and Implementation of Typed Scheme | booktitle = Principles of Programming Languages | year = 2008}} 6. ^{{cite conference | title = Interlanguage Migration: From Scripts to Programs | booktitle = Dynamic Languages Symposium | last1 = Tobin-Hochstadt | first1 = S. | last2 = Felleisen | first2 = M. | year = 2006}} 7. ^http://docs.racket-lang.org/plai/plai-scheme.html?q=plai 8. ^Krishnamurthi, Shriram. "Programming Languages: Application and Interpretation." Programming Languages: Application and Interpretation. Brown University, n.d. Web. 14 Mar. 2016. 9. ^Note that #%app is a macro that is used in all function calls, making this language not too efficient, as every function call incurs an additional condition. In addition, the macro evaluates the function expression twice, so it should not be taken as an example of good macro-programming.
External links
3 : Articles with example Racket code|Software development|Language workbench |
随便看 |
|
开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。