Safekipedia

Racket (programming language)

Discoverer · ages 10+Adapted from Wikipedia
A Sierpinski triangle, a famous fractal pattern drawn by a short computer program written in the Racket language.

Racket is a general-purpose, multi-paradigm programming language. It is a modern version of Lisp and a descendant of Scheme. Racket was designed as a platform for programming language design and implementation. The word "Racket" also refers to the whole family of related languages and the tools that help people build software with them. People also use Racket for scripting, for teaching computer science, and for research.

The Racket platform includes an implementation of the language, with a runtime system, libraries, and a compiler. The compiler supports several modes: machine code, cross-platform, interpreted, and just-in-time compilation (JIT). The platform also includes DrRacket, an integrated development environment (IDE) that is itself written in Racket. Racket is used by the ProgramByDesign outreach program, which aims to make computer science "an indispensable part of the liberal arts curriculum".

The core Racket language is known for its powerful macro system. This system makes it possible to create embedded and domain-specific languages, to build language features such as classes or modules, and to make separate dialects of Racket with different semantics.

The Racket platform is free and open-source software. It is distributed under the Apache 2.0 and MIT licenses. People in the community can upload their own extensions and packages to Racket's package catalog.

History

Matthias Felleisen founded PLT Inc. in the mid 1990s. It began as a research group and soon became a project that made pedagogic materials for new programmers. In January 1995, the group decided to build a teaching programming environment based on Scheme. Matthew Flatt put together MrEd, the first virtual machine for Racket, from libscheme, wxWidgets, and a few other free systems. Flatt, Robby Findler, Shriram Krishnamurthi, and others then built DrScheme, a programming environment for new Scheme programmers and a research tool for gradual typing. Over the years, the team added many features to it, including a transparent read–eval–print loop. By 2001, the core team had also published the textbook How to Design Programs.

Early versions of PLT Scheme added features for programming in the large, including modules and classes. Version 42 added units, and the class system gained Java-style interfaces while losing some features, such as multiple inheritance. Version 200 brought a new default module system that keeps compile-time and run-time work separate, and its modules are not first-class objects. Version 300 added Unicode and foreign library support and improved performance with a JIT compiler and generational garbage collection. Version 4.0 added sequence-based version numbers, immutable pairs and lists, parallelism, and a statically-typed dialect. On 7 June 2010, PLT Scheme was renamed Racket with Version 5.0. Version 5.1 rewrote the graphical user interface backend from C++ using native UI toolkits, and later versions added a syntax checking tool, optimization tools, and a JSON library. Racket 8.0, released on 13 February 2021, made Racket CS, which uses the Chez Scheme runtime system, the default version.

Features

Main article: Racket features

Racket's core language includes macros, modules, lexical closures, tail calls, delimited continuations, parameters (fluid variables), software contracts, green threads and OS threads, and more. Its built-in tools, such as eventspaces and custodians, manage resources and let the language act like an operating system for loading and running other programs. Most language features are built as macros, including a mixin class system, a component system as expressive as the one in the ML module system, and pattern matching. Racket also has the first contract system for a higher-order programming language. It is inspired by the Design by Contract work for Eiffel and works with higher-order values such as first-class functions, objects, reference cells, and more. Racket includes both bytecode and JIT compilers. The bytecode compiler makes a format run by the Racket virtual machine, and the JIT compiler turns bytecode into machine code while the program runs.

Since 2004, Racket has shipped with PLaneT, a package manager built into the module system so that third-party libraries can be easily imported and used. PLaneT also has a built-in versioning policy to help prevent dependency hell. At the end of 2014, much of Racket's code was moved into a separate packaging system, served by a program named raco. This new system has fewer features than PLaneT.

See also: Racket language extensions

Racket is distinguished from other Lisp-family languages by its built-in extensibility features, which support building new domain-specific and general-purpose languages. These features are built into the module system, so programmers can control the semantics of function application and analyze an entire module. Since any module can be used as a language through the #lang notation, almost any part of the language can be programmed and controlled. These features combine with a Scheme-like hygienic macro system that offers more than Lisp's s-expression manipulation system, Scheme 84's hygienic extend-syntax macros, or R5RS's syntax-rules. The macro system is a carefully tuned application programming interface (API) for compiler extensions, letting programmers add features and entire domain-specific languages that seem just like built-in parts of the language. The macro system has been used to build several language dialects, including Typed Racket, a untyped-to-typed dialect; Lazy Racket, which uses lazy evaluation; and Hackett, which combines Haskell and Racket. The teaching language Pyret was first built in Racket. Other dialects include FrTime (functional reactive programming), Scribble (a documentation language), Slideshow (a presentation language), and several languages for education. Racket's core distribution also provides libraries for building programming languages that are not limited to s-expression syntax. See Racket logic programming for an example.

Programming environment

The Racket platform includes a self-hosted IDE called DrRacket, a web server based on continuations, a graphical user interface, and other tools. With libraries like those in common scripting languages, it can be used to script the Unix shell. It can read command-line arguments and run external tools.

DrRacket (formerly DrScheme) is widely used in beginner computer science courses that teach Scheme or Racket, and it is praised for being simple and appealing to new programmers. It was originally built for the TeachScheme! project (now ProgramByDesign), an outreach effort by Northeastern University and several affiliated universities to bring high school students into college-level computer science courses. The editor offers highlighting for code and errors, a debugger, and analysis tools for advanced programmers. It also supports module-oriented programming, testing, coverage measurements, and refactoring, and it includes a help system called "Help Desk". DrRacket runs on Windows, macOS, Unix, and Linux with the X Window System, and programs behave similarly on all of these platforms.

Code examples

Here is a very simple "Hello, World!" program. Running it produces the output "Hello, World!"

Here is a slightly more complex program:

#lang racket
(require 2htdp/image)

<figure class="inline-figure"><img src="https://thumb.wikimedia.org/wikipedia/commons/thumb/8/8c/Sierpinski_Racket_example.png/250px-Sierpinski_Racket_example.png?utm_source=en.wikipedia.org&utm_campaign=parser&utm_content=thumbnail" data-caption="The result of this program, as shown in DrRacket"><figcaption>The result of this program, as shown in DrRacket</figcaption></figure>

(let sierpinski ([n 8])
  (if (zero? n)
    (triangle 2 'solid 'red)
    (let ([t (sierpinski (- n 1))])
      (freeze (above t (beside t t))))))

This program, taken from the Racket website, draws a Sierpinski triangle, nested to depth 8.

The #lang line lets a source file be written in different dialects of Racket. Here is the factorial program in Typed Racket, a statically typed dialect:

#lang typed/racket

(: fact (Integer -> Integer))
(define (fact n)
  (if (zero? n) 1 (* n (fact (- n 1)))))

Implementations

Racket currently has two versions that can run it. Both work on Linux, Windows and macOS, on many kinds of computer processors, and are supported as of version 8.8 (2023). The default version uses the Chez Scheme compiler and runtime. The other version creates code that is not tied to one type of computer, and it uses just-in-time compilation to make machine code as the code is loaded.

There are also two experimental versions. RacketScript turns Racket code into JavaScript, and it aims to let programmers use the tools of both languages together. Pycket is a version of Racket made with the RPython framework.

Applications and practical use

Racket was designed as a general-purpose language for production systems, and it has a basis in programming language theory. Its distribution includes a large library that covers systems and network programming, web development, a uniform interface to the operating system, a dynamic foreign function interface, several kinds of regular expressions, lexer/parser generators, logic programming, and a complete GUI framework.

Racket has useful features for business use, such as the ability to build standalone programs on Windows, macOS, and Unix. It also includes a profiler and debugger in its integrated development environment (IDE), along with a unit testing framework. Racket has been used for commercial projects and web applications. For example, the Hacker News website runs on Arc, which is developed in Racket. Naughty Dog has used Racket as a scripting language in several video games. It is also used to teach students algebra through game design in the Bootstrap program.

Quiz time!

5 questions about Racket (programming language)

Question 1 of 5

What kind of programming language is Racket?

Keep exploring

This article is a child-friendly adaptation of the Wikipedia article on Racket (programming language), available under CC BY-SA 4.0.

Images from Wikimedia Commons. Tap any image to view credits and license.