Programming Language Manuals
Collection
5 items • Updated
text stringlengths 12 127k |
|---|
# The Racket Guide
8.18.0.13 |
# The Racket Guide
This guide is intended for programmers who are new to Racket or new to some part of Racket. It assumes programming experience, so if you are new to programming, consider instead reading [How to Design Programs](https://htdp.org). If you want an especially quick introduction to Racket, start with [Qui... |
# The Racket Guide
| |
|---------------------------------------------------------------------------------------------------------------------... |
# The Racket Guide
------------------------------------------------------------------------ |
# 1 Welcome to Racket |
## 1 Welcome to Racket
Depending on how you look at it, Racket is
- a programming language—a dialect of Lisp and a descendant of Scheme;
> > > See [Dialects of Racket and Scheme](dialects.html) for more information on other dialects of Lisp and how they relate to Racket.
- a family of programming languages—vari... |
# 1 Welcome to Racket
in DrRacket’s top text area, and then click the Run button that’s above the text area. DrRacket then understands that you mean to work in the normal variant of Racket (as opposed to the smaller [racket/base](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?... |
### 1.1 Interacting with Racket
DrRacket’s bottom text area and the racket command-line program (when started with no options) both act as a kind of calculator. You type a Racket expression, hit the Return key, and the answer is printed. In the terminology of Racket, this kind of calculator is called a read-eval-print ... |
### 1.2 Definitions and Interactions
You can define your own functions that work like [substring](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._substring%2529%2529&version=8.18.0.13) by using t... |
### 1.2 Definitions and Interactions
> ```racket
> #lang racket
> ( define ( extract str )
> ( substring str 4 7 ) )
> ```
If calling (extract "the boy") is part of the main action of your program, that would go in the [definitions area](#%28tech._definitions._area%29), too. But if it was just an example expression tha... |
### 1.3 Creating Executables
If your file (or [definitions area](#%28tech._definitions._area%29) in DrRacket) contains
> ```racket
> #lang racket
> ( define ( extract str )
> ( substring str 4 7 ) )
> ( extract "the cat out of the bag" )
> ```
then it is a complete program that prints “cat” when run. You can run the pr... |
### 1.3 Creating Executables
The script works as long as racket is in the user’s executable search path. Alternately, use a full path to racket after #! (with a space between #! and the path), in which case the user’s executable search path does not matter. |
### 1.4 A Note to Readers with Lisp/Scheme Experience
If you already know something about Scheme or Lisp, you might be tempted to put just
> ```racket
> ( define ( extract str )
> ( substring str 4 7 ) )
> ```
into "extract.rktl" and run racket with
> ```racket
> > ( load "extract.rktl" )
> > ( extract "the dog out" )
... |
# 2 Racket Essentials |
## 2 Racket Essentials
This chapter provides a quick introduction to Racket as background for the rest of the guide. Readers with some Racket experience can safely skip to [Built-In Datatypes](datatypes.html).
| ... |
# 2 Racket Essentials
------------------------------------------------------------------------ |
# 2.1 Simple Values |
### 2.1 Simple Values
Racket values include numbers, booleans, strings, and byte strings. In DrRacket and documentation examples (when you read the documentation in color), value expressions are shown in green.
Numbers are written in the usual way, including fractions and imaginary numbers:
> > > <img src="finger.png" ... |
# 2.1 Simple Values
When a constant is evaluated in the [REPL](intro.html#%28tech._repl%29), it typically prints the same as its input syntax. In some cases, the printed form is a normalized version of the input syntax. In documentation and in DrRacket’s [REPL](intro.html#%28tech._repl%29), results are printed in blue ... |
# 2.2 Simple Definitions and Expressions |
### 2.2 Simple Definitions and Expressions
A program module is written as
> #lang ‹langname› ‹topform›\*
where a ‹topform› is either a ‹definition› or an ‹expr›. The [REPL](intro.html#%28tech._repl%29) also evaluates ‹topform›s.
In syntax specifications, text with a gray background, such as #lang, represents literal te... |
#### 2.2.1 Definitions
A definition of the form
> > > <img src="finger.png" width="24" height="24" alt="+" />[Definitions: define](define.html) (later in this guide) explains more about definitions.
> ( define ‹id› ‹expr› )
binds ‹id› to the result of ‹expr›, while
> ( define ( ‹id› ‹id›\* ) ‹expr›+ )
binds the first ‹... |
# 2.2 Simple Definitions and Expressions
Racket programmers prefer to avoid side-effects, so a definition usually has just one expression in its body. It’s important, though, to understand that multiple expressions are allowed in a definition body, because it explains why the following nobake function fails to include ... |
#### 2.2.2 An Aside on Indenting Code
Line breaks and indentation are not significant for parsing Racket programs, but most Racket programmers use a standard set of conventions to make code more readable. For example, the body of a definition is typically indented under the first line of the definition. Identifiers are... |
# 2.2 Simple Definitions and Expressions
> ```racket
> ( define ( halfbake flavor
> ( string-append flavor " creme brulee" ) ) )
> ```
In this case, indentation helps highlight the mistake. In other cases, where the indentation may be normal while an open parenthesis has no matching close parenthesis, both racket and D... |
#### 2.2.3 Identifiers
Racket’s syntax for identifiers is especially liberal. Excluding the special characters
> > > <img src="finger.png" width="24" height="24" alt="+" />[Identifiers and Binding](binding.html) (later in this guide) explains more about identifiers.
( ) \[ \] { } " , ' \` ; # \| \\
and except for th... |
#### 2.2.4 Function Calls (Procedure Applications)
We have already seen many function calls, which are called procedure applications in more traditional terminology. The syntax of a function call is
> > > <img src="finger.png" width="24" height="24" alt="+" />[Function Calls](application.html) (later in this guide) exp... |
# 2.2 Simple Definitions and Expressions
> ```racket
> > ( string-append "rope" "twine" "yarn" ) ; append strings
> "ropetwineyarn"
> > ( substring "corduroys" 0 4 ) ; extract a substring
> "cord"
> > ( string-prefix? "shoelace" "shoe" ) ; recognize string prefix/suffix
> #t
> > ( string-suffix? "shoelace" "shoe" )
> #... |
#### 2.2.5 Conditionals with [if](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._if%2529%2529&version=8.18.0.13), [and](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect... |
# 2.2 Simple Definitions and Expressions
The first ‹expr› is always evaluated. If it produces a non-#f value, then the second ‹expr› is evaluated for the result of the whole [if](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528qu... |
# 2.2 Simple Definitions and Expressions
Complex conditionals can be formed by nesting [if](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._if%2529%2529&version=8.18.0.13) expressions. For example, i... |
# 2.2 Simple Definitions and Expressions
but these kinds of nested [if](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._if%2529%2529&version=8.18.0.13)s are difficult to read. Racket provides more re... |
# 2.2 Simple Definitions and Expressions
The [and](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._and%2529%2529&version=8.18.0.13) form short-circuits: it stops and returns #f... |
# 2.2 Simple Definitions and Expressions
> ```racket
> ( define ( reply-only-enthusiastic s ) ( if ( and ( string? s ) ( string-prefix? s "hello " ) ( string-suffix? s "!" ) ) "hi!" "huh?" ) )
> > ( reply-only-enthusiastic "hello racket!" )
> "hi!"
> > ( reply-only-enthusiastic "hello racket" )
> "huh?"
> ```
Another c... |
# 2.2 Simple Definitions and Expressions
A [cond](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._cond%2529%2529&version=8.18.0.13) form contains a sequence of clauses between ... |
# 2.2 Simple Definitions and Expressions
> ( cond
> [ ( string-prefix? s "hello " )
> "hi!" ]
> [ ( string-prefix? s "goodbye " )
> "bye!" ]
> [ ( string-suffix? s "?" )
> "I don't know" ]
> [ else "huh?" ] ) )
> ( reply-more "hello racket" )
> "hi!"
> > ( reply-more "goodbye cruel world" )
> "bye!"
> > ( reply-more "w... |
# 2.2 Simple Definitions and Expressions
If the derivation of the above definitions is mysterious to you, consider reading How to Design Programs. If you are merely suspicious of the use of recursive calls instead of a looping construct, then read on. |
#### 2.3.3 Tail Recursion
Both the my-length and my-map functions run in O(n) space for a list of length n. This is easy to see by imagining how (my-length (list "a" "b" "c")) must evaluate:
> ```racket
> ( my-length ( list "a" "b" "c" ) )
> = ( + 1 ( my-length ( list "b" "c" ) ) )
> = ( + 1 ( + 1 ( my-length ( list "c... |
# 2.2 Simple Definitions and Expressions
The revised my-length runs in constant space, just as the evaluation steps above suggest. That is, when the result of a function call, like (iter (list "b" "c") 1), is exactly the result of some other function call, like (iter (list "c") 2), then the first one doesn’t have to wa... |
# 2.2 Simple Definitions and Expressions
then the for/list form in the function is expanded to essentially the same code as the iter local definition and use. The difference is merely syntactic convenience. |
#### 2.3.4 Recursion versus Iteration
The my-length and my-map examples demonstrate that iteration is just a special case of recursion. In many languages, it’s important to try to fit as many computations as possible into iteration form. Otherwise, performance will be bad, and moderately large inputs can lead to stack ... |
# 2.2 Simple Definitions and Expressions
> ( remove-dups ( list "a" "b" "b" "b" "c" "c" ) )
> '("a" "b" "c")```
In general, this function consumes O(n) space for an input list of length n, but that’s fine, since it produces an O(n) result. If the input list happens to be mostly consecutive duplicates, then the resultin... |
# 2.4 Pairs, Lists, and Racket Syntax |
### 2.4 Pairs, Lists, and Racket Syntax
The [cons](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._cons%2529%2529&version=8.18.0.13) function actually accepts any two values, not just a list for th... |
# 2.4 Pairs, Lists, and Racket Syntax
Thus, a value produced by [cons](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._cons%2529%2529&version=8.18.0.13) is not always a list. In general, the result... |
# 2.4 Pairs, Lists, and Racket Syntax
The name [rest](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528lib._racket%252Flist..rkt%2529._rest%2529%2529&version=8.18.0.13) also makes less sense for non-list pairs; the more traditio... |
# 2.4 Pairs, Lists, and Racket Syntax
> ```racket
> > ( car ( cons 1 2 ) )
> 1
> > ( cdr ( cons 1 2 ) )
> 2
> > ( pair? empty )
> #f
> > ( pair? ( cons 1 2 ) )
> #t
> > ( pair? ( list 1 2 3 ) )
> #t
> ```
Racket’s pair datatype and its relation to lists is essentially a historical curiosity, along with the dot notation... |
# 2.4 Pairs, Lists, and Racket Syntax
Non-list pairs are used intentionally, sometimes. For example, the [make-hash](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=hashtables.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._make-hash%2529%2529&version... |
#### 2.4.1 Quoting Pairs and Symbols with [quote](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=quote.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._quote%2529%2529&version=8.18.0.13)
A list prints with a quote mark before it, but if an element of... |
# 2.4 Pairs, Lists, and Racket Syntax
> ```racket
> > ( list ( list 1 2 3 ) 5 ( list "a" "b" "c" ) )
> '((1 2 3) 5 ("a" "b" "c"))
> > ( quote ( ( 1 2 3 ) 5 ( "a" "b" "c" ) ) )
> '((1 2 3) 5 ("a" "b" "c"))
> ```
If you wrap an identifier with [quote](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-... |
# 2.4 Pairs, Lists, and Racket Syntax
Indeed, the intrinsic value of a symbol is nothing more than its character content. In this sense, symbols and strings are almost the same thing, and the main difference is how they print. The functions [symbol->string](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/do... |
# 2.4 Pairs, Lists, and Racket Syntax
In the same way that [quote](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=quote.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._quote%2529%2529&version=8.18.0.13) for a list automatically applies itself to nes... |
#### 2.4.2 Abbreviating [quote](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=quote.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._quote%2529%2529&version=8.18.0.13) with '
As you may have guessed, you can abbreviate a use of [quote](https://plt.c... |
# 2.4 Pairs, Lists, and Racket Syntax
A ' expands to a [quote](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=quote.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._quote%2529%2529&version=8.18.0.13) form in quite a literal way. You can see this if y... |
#### 2.4.3 Lists and Racket Syntax
Now that you know the truth about pairs and lists, and now that you’ve seen [quote](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=quote.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._quote%2529%2529&version=8.18.... |
# 2.4 Pairs, Lists, and Racket Syntax
This works because ([+](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._%252B%2529%2529&version=8.18.0.13) 1 . (2)) is just another way of writing ([... |
# 2.4 Pairs, Lists, and Racket Syntax
This two-dot convention is non-traditional, and it has essentially nothing to do with the dot notation for non-list pairs. Racket programmers use the infix convention sparingly—mostly for asymmetric binary operators such as [<](https://plt.cs.northwestern.edu/new-snapshots/new-snap... |
# 3 Built-In Datatypes |
## 3 Built-In Datatypes
The [previous chapter](to-scheme.html) introduced some of Racket’s built-in datatypes: numbers, booleans, strings, lists, and procedures. This section provides a more complete coverage of the built-in datatypes for simple forms of data.
| ... |
# 3.1 Booleans |
### 3.1 Booleans
Racket has two distinguished constants to represent boolean values: #t for true and #f for false. Uppercase #T and #F are parsed as the same values, but the lowercase forms are preferred.
The [boolean?](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=refere... |
# 3.1 Booleans
> ```racket
> > ( = 2 ( + 1 1 ) )
> #t
> > ( boolean? #t )
> #t
> > ( boolean? #f )
> #t
> > ( boolean? "no" )
> #f
> > ( if "no" 1 0 )
> 1
> ```
------------------------------------------------------------------------ |
# 3.2 Numbers |
### 3.2 Numbers
A Racket number is either exact or inexact:
- An exact number is either
- an arbitrarily large or small integer, such as 5, 99999999999999999, or -17;
- a rational that is exactly the ratio of two arbitrarily small or large integers, such as 1/2, 99999999999999999/2, or -3/4; or
- a ... |
# 3.2 Numbers
> > > <img src="magnify.png" width="24" height="24" alt="+" />[Reading Numbers](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=reader.html%23%2528part._parse-number%2529&version=8.18.0.13) in [The Racket Reference](https://plt.cs.northwestern.ed... |
# 3.2 Numbers
Inexact results are also produced by procedures such as [sqrt](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._sqrt%2529%2529&version=8.18.0.13), [log](https://plt.cs.northw... |
# 3.2 Numbers
> ( time ( round ( sigma ( lambda ( x ) ( / 1 x ) ) 1 2000 ) ) )
> cpu time: 21 real time: 3 gc time: 0
> 8
> > ( time ( round ( sigma ( lambda ( x ) ( / 1.0 x ) ) 1 2000 ) ) )
> cpu time: 0 real time: 0 gc time: 0
> 8.0
```
(Unicode) →
(-> integer → char 65)```
The above example says that, within a so... |
# 3.2 Numbers
For example,
> (go "super.rkt" #:mode 'fast)
calls the function bound to go with "super.rkt" as a by-position argument, and with 'fast as an argument associated with the #:mode keyword. A keyword is implicitly paired with the expression that follows it.
Since a keyword by itself is not an expression, then... |
#### 4.3.3 The apply Function
The syntax for function calls supports any number of arguments, but a specific call always specifies a fixed number of arguments. As a result, a function that takes a list of arguments cannot directly apply a function like + to all of the items in a list:
> ```
(define (avg lst) ; doesn’t ... |
# 3.2 Numbers
```
The apply function offers a way around this restriction. It takes a function and a list argument, and it applies the function to the values in the list:
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><pre><code>( define ( avg lst )
> ... |
# 3.2 Numbers
That is, a lambda expression can have a single rest-id that is not surrounded by parentheses. The resulting function accepts any number of arguments, and the arguments are put into a list bound to rest-id.
Examples:
> ```racket
> > ( ( lambda x x ) 1 2 3 )
> '(1 2 3)
> > ( ( lambda x x ) )
> '()
> > ( ( l... |
#### 4.4.2 Declaring Optional Arguments
Instead of just an identifier, an argument (other than a rest argument) in a [lambda](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=lambda.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._lambda%... |
# 3.2 Numbers
An argument specified as arg-keyword arg-id is supplied by an application using the same arg-keyword. The position of the keyword–identifier pair in the argument list does not matter for matching with arguments in an application, because it will be matched to an argument value by keyword instead of by pos... |
# 3.2 Numbers
The lambda form does not directly support the creation of a function that accepts “rest” keywords. To construct a function that accepts all keyword arguments, use make-keyword-procedure. The function supplied to make-keyword-procedure receives keyword arguments through parallel lists in the first two (by-... |
#### 4.4.4 Arity-Sensitive Functions: case-lambda
The case-lambda form creates a function that can have completely different behaviors depending on the number of arguments that are supplied. A case-lambda expression has the form
> > ```
(case-lambda
> > [ formals body ...+ ]
> > ... )
formals = (arg-id ...)
| ... |
# 3.2 Numbers
> ```racket
> ( define greet ( case-lambda [ ( name ) ( string-append "Hello, " name ) ] [ ( given surname ) ( string-append "Hello, " given " " surname ) ] ) )
> > ( greet "John" )
> "Hello, John"
> > ( greet "John" "Smith" )
> "Hello, John Smith"
> > ( greet )
> greet: arity mismatch;
> the expected num... |
# 4.5 Definitions: define |
### 4.5 Definitions: [define](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=8.18.0.13)
A basic definition has the form
> > | ... |
#### 4.5.1 Function Shorthand
The [define](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=8.18.0.13) form also supports a shorthand for function definition... |
### 4.5 Definitions: [define](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=8.18.0.13)
> ([define](https://plt.cs.northwestern.edu/new-snapshots/new-snaps... |
### 4.5 Definitions: [define](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=8.18.0.13)
```
The function shorthand via define also supports a rest argument... |
### 4.5 Definitions: [define](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=8.18.0.13)
so the following are valid instances of the grammar:
> ```racket
> ... |
### 4.5 Definitions: [define](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=8.18.0.13)
> > > <img src="magnify.png" width="24" height="24" alt="+" />Inter... |
# 4.6 Local Binding |
### 4.6 Local Binding
Although internal defines can be used for local binding, Racket provides three forms that give the programmer more control over bindings: let, let*, and letrec. |
This dataset contains the Racket programming language documentation, chunked using semantic parsing for pretraining language models.
Updated: 2025-09-08
from datasets import load_dataset
ds = load_dataset("json", data_files={"train": "train.jsonl"}, split="train")
text field per line