Racket Basics and Style
Last updated: Mon, 26 Aug 2024 17:48:20 -0400
This page gives a basic introduction to the Racket programming language.
The Racket documentation and the Textbook include many more details and will be useful resources this semester.
1 Installation
See Racket’s "Getting Started Page"
2 Programming Basics
All Racket files end with a .rkt extension, e.g., hw0.rkt.
Every Racket file (in this class) must begin with #lang racket as the first line.
This specifies that the code that follows is written in the main racket language (Racket is actually an entire collection of different programming languages, but for this course we will stick to #lang racket only.)
Identifiers in Racket can use more characters than other languages typically allow. (Also, they are case sensitive!)
Some non-traditional characters commonly used in Racket identifiers are -, ?, !, and /.
See this page for a complete list of allowed/disallowed characters in an identifier.
Comment lines begin with a semicolon (;) (see commenting style in the next section).
Alternatively, s-expressions may be commented with a "hash-semicolon" (#;).
3 Racket Style
Code is read much more often than it is written (well over 10 to 1). Thus readability is one of the most important aspects of code.
To be most readable, however, all code must be written according to some accepted list of style guidelines.
In this course, we will use the Racket Style Guide.
Here are a few conventions that we’ll use more frequently:
Spacing: Closing parentheses do not go on their own line.
Spacing: Indentation matters. When in doubt, use DrRacket’s auto-formatter.
Code Organization: provides and requires go at the top of the file (provides first).
Brackets have the same meaning as parens but for readability, we will use them in some situations, e.g., around cond clauses.
Comments: double semicolon for full-line comments, single for partial line.
Naming: Predicate (function that returns a boolean) names have a ? suffix, e.g., string?.
Naming: multi-word identifiers, e.g. string-append, use - as the separator (not underscore or camelCase)
No "magic numbers". Define constants instead.
Naming: All constants must be named with an all-caps identifier.
4 The Design Recipe
All code in this class must follow The Design Recipe.