




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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 ...
Typology: Lecture notes
1 / 403
This page cannot be seen from the preview
Don't miss anything!





























































































Version 8.5.0.
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. If you want an especially quick introduction to Racket, start with Quick: An Introduction to Racket with Pictures. Chapter 2 provides a brief introduction to Racket. From Chapter 3 on, this guide dives into details—covering much of the Racket toolbox, but leaving precise details to The Racket Reference and other reference manuals. The source of thismanual is available on GitHub.
Depending on how you look at it, Racket is
Where there is no room for confusion, we use simply Racket. Racket’s main tools are
Most likely, you’ll want to explore the Racket language using DrRacket, especially at the beginning. If you prefer, you can also work with the command-line racket interpreter and your favorite text editor; see also §24 “Command-Line Tools and Your Editor of Choice”. The rest of this guide presents the language mostly independent of your choice of editor. If you’re using DrRacket, you’ll need to choose the proper language, because DrRacket accommodates many different variants of Racket, as well as other languages. Assuming that you’ve never used DrRacket before, start it up, type the line #lang racket
in DrRacket’s top text area, and then click the Run button that’s above the text area. Dr- Racket then understands that you mean to work in the normal variant of Racket (as opposed to the smaller racket/base or many other possibilities). §23.1 “MoreRackets” describes If you’ve used DrRacket before with something other than a program that starts #lang, some of the otherpossibilities. DrRacket will remember the last language that you used, instead of inferring the language from the #lang line. In that case, use the Language|Choose Language... menu item. In the dialog that appears, select the first item, which tells DrRacket to use the language that is declared in a source program via #lang. Put the #lang line above in the top text area, still.
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 loop or REPL. A number by itself is an expression, and the answer is just the number:
5 5
A string is also an expression that evaluates to itself. A string is written with double quotes at the start and end of the string:
"Hello, world!" "Hello, world!"
Racket uses parentheses to wrap larger expressions—almost any kind of expression, other than simple constants. For example, a function call is written: open parenthesis, function name, argument expression, and closing parenthesis. The following expression calls the built-in function substring with the arguments "the boy out of the country", 4 , and 7 :
(substring "the boy out of the country" 4 7) "boy"
You can define your own functions that work like substring by using the define form, like this: (define (extract str) (substring str 4 7))
(extract "the boy out of the country") "boy" (extract "the country out of the boy") "cou"
Although you can evaluate the define form in the REPL, definitions are normally a part of a program that you want to keep and use later. So, in DrRacket, you’d normally put the definition in the top text area—called the definitions area—along with the #lang prefix:
If you already know something about Scheme or Lisp, you might be tempted to put just (define (extract str) (substring str 4 7))
into "extract.rktl" and run racket with
(load "extract.rktl") (extract "the dog out") "dog"
That will work, because racket is willing to imitate a traditional Lisp environment, but we strongly recommend against using load or writing programs outside of a module. Writing definitions outside of a module leads to bad error messages, bad performance, and awkward scripting to combine and run programs. The problems are not specific to racket; they’re fundamental limitations of the traditional top-level environment, which Scheme and Lisp implementations have historically fought with ad hoc command-line flags, compiler directives, and build tools. The module system is designed to avoid these problems, so start with #lang, and you’ll be happier with Racket in the long run.
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 §3 “Built-In Datatypes”.
Racket values include numbers, booleans, strings, and byte strings. In DrRacket and doc- umentation 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: §3.2 “Numbers”(later in this guide) 1 3.14 explains more aboutnumbers. 1/2 6.02e+ 1+2i 9999999999999999999999
Booleans are #t for true and #f for false. In conditionals, however, all non-#f values are treated as true. §3.1 “Booleans”(later in this guide) Strings are written between doublequotes. Within a string, backslash is an escaping char- explains more aboutbooleans. acter; for example, a backslash followed by a doublequote includes a literal doublequote in the string. Except for an unescaped doublequote or backslash, any Unicode character can appear in a string constant. §3.4 “Strings(Unicode)” (later in "Hello, world!" this guide) explainsmore about strings. "Benjamin "Bugsy" Siegel" "λx:(μα.αÑα).xx"
When a constant is evaluated in the REPL, it typically prints the same as its input syntax. In some cases, the printed form is a normalized version of the input syntax. In documen- tation and in DrRacket’s REPL, results are printed in blue instead of green to highlight the difference between an input expression and a printed result. Examples:
"Bugs \u0022Figaro\u0022 Bunny" "Bugs "Figaro" Bunny"