iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🎾

Racket: How to Install and Configure Racket on Windows

に公開
2024/04/16

Introduction

In this article, I will explain how to install Racket on Windows and perform basic configuration.
After setup, you can launch Racket from the terminal and start learning functional programming immediately.

1. About Racket

1.1 Purpose

In this section, I will explain the features of Racket.

1.2 Features

  • A functional programming language
  • A powerful macro system that allows defining new language features within a program
  • Support for the official integrated development environment, DrRacket
  • Support for development tasks via the command-line tool, raco

2. Prerequisites

2.1 Installation Directory

Normally, Racket is installed in C:\Program Files\Racket, but to avoid Paths containing spaces, this article installs it in c:\lang\racket.

2.2 Configuration Directories

Configuration directories are placed according to the XDG Base Directory specification.
Usually, configuration files are saved under $USERPROFILE (C:\Users\<username>), but this location contains various Windows folders and application setting folders.
To avoid confusion with these folders, we will place files in ~\.config\racket following the XDG Base Directory specification.

Other environment configuration directories will also be changed to match the above settings.
For details on which directories are changed, please refer to Racket Configuration Files/Directories Summary.

3. Installing Racket

We will install Racket using winget, the official Windows package manager. To do this, the Windows Package Manager tool must be installed on your system.

3.1 Installing Racket using winget

winget is the official package manager for Windows, allowing you to install Racket from the command line. To install it under c:\lang\racket, specify the installation destination directory with the --location option.

Install Racket using the following command:

winget install Racket.Racket --location C:\lang\racket

After installation, verify that it was installed correctly with racket --version:

c:\lang\racket\racket --version

If the Racket version is displayed as follows, it has been installed successfully.

Welcome to Racket v8.12 [cs].

3.2 Path Configuration

To allow Racket to be launched from any directory, add c:\lang\racket to the Path environment variable. Run the following command in PowerShell:

[System.Environment]::SetEnvironmentVariable("Path", [System.Environment]::GetEnvironmentVariable("Path", "Machine")+";c:\lang\racket", "Machine")

4. Racket Configuration

4.1 Environment Variable Configuration

To make the Racket configuration files compliant with the XDG Base Directory specification, set the related environment variables.

The environment variable settings are as follows:

Environment Variable Variable Description Setting Value Description
PLTUSERHOME Home directory for Racket $XDG_CONFIG_HOME+"/racket" Saves configuration files, etc.
PLTADDONDIR User add-on directory $XDG_DATA_HOME+"/racket" Saves add-ons downloaded for the user

Set the environment variables with the following command:

[System.Environment]::SetEnvironmentVariable("PLTUSERHOME", $env:XDG_CONFIG_HOME+"/racket", "User")
[System.Environment]::SetEnvironmentVariable("PLTADDONDIR", $env:XDG_DATA_HOME+"/racket", "User")

4.2 config.rktd Configuration

Racket has a configuration file called config.rktd, which allows you to manage various Racket settings. Here, we will configure the directory for caching files when downloading add-ons to comply with the XDG Base Directory specification.

Location of the config file config.rktd: c:\lang\racket\etc\config.rktd
Edit config.rktd as follows:

config.rkd
#hash(
  (build-stamp . "")
  (catalogs . ("https://download.racket-lang.org/releases/8.12/catalog/" #f))
  (doc-search-url . "https://download.racket-lang.org/releases/8.12/doc/local-redirect/index.html")
  (default-scope . "user")
  (download-cache-dir . "C:/Users/atsushifx/.local/cache/racket/download-cache") ;; ← Add this
 )

By doing the above, you prevent cache files from being saved under the ~/.config directory and keep the system clean.

Note:

  • Since download-cache-dir needs to be written as a full path, C:/Users/<username>/.local/cache is used to expand XDG_CACHE_HOME, followed by adding /racket/download-cache.
  • Because the configuration includes the Racket version number, you will need to rewrite config.rktd if Racket is updated.

4.3 .gitignore Configuration

The Racket home directory is managed under the dotfiles repository on GitHub. The Racket home directory contains configuration files, user setting files, and the download cache for add-ons. Among these, user setting files containing Racket session information and the download cache containing temporary files should be excluded from Git management. To achieve this, add settings to exclude the above files to .gitignore under $XDG_CONFIG_HOME.

Add the following content to .gitignore:

.gitignore
# Racket
_lock*
**/download-cache/
racket-prefs.rktd

4.4 Path Configuration for Add-ons

Some add-ons create launchers for execution. For user add-ons, launchers are created under the add-on directory + the Racket version number. In this article, since we installed Racket 8.12, it will be $PLTADDONDIR+"/8.12".

Add the Path for add-ons to the environment variable Path with the following command:

[System.Environment]::SetEnvironmentVariable("Path", [System.Environment]::GetEnvironmentVariable("Path", "User")+";"+$env:PLTADDONDIR+"/8.12", "User")

4.5 Restarting the Terminal

The Path and environment variables set will not be immediately reflected in the current PowerShell session. You need to start a new terminal to use a PowerShell session where the settings have been applied.

Restart the terminal using the following steps:

  1. Exit powershell:
    Run the following command to exit powershell.

    exit
    
  2. Launch Terminal:
    Press [Windows+R] to open the [Run] dialog, type wt, and press Enter to launch the terminal.

    wt
    

5. Launching and Closing Racket on Windows

5.1 Launching Racket

Follow these steps to launch Racket. Enter the following command in the terminal to launch Racket:

racket

If Racket launches successfully, the REPL will start, and the following message and prompt will be displayed:

> racket
Welcome to Racket v8.12 [cs].
>

5.2 Closing Racket

When you launch Racket without specifying a file, the REPL starts. You can exit the REPL in the following ways:

  • Entering EOF (Ctrl+D):
    The REPL ends when EOF is entered. EOF can be entered with Ctrl+D.

    Welcome to Racket v8.12 [cs].
    > [Press Ctrl+D key]
    
    $
    
  • Executing the exit function:
    Execute the exit function to close Racket. Since it is called as a function, it must be enclosed in ().

    Welcome to Racket v8.12 [cs].
    > (exit)
    
    $
    
  • Executing the exit command:
    In XREPL, you can execute commands in the ,<command> format. The exit command is ,exit.

    Welcome to Racket v8.12 [cs].
    > ,exit
    
    $
    

Conclusion

So far, I have explained the installation, environment configuration, launching, and closing of Racket.
As a result, you are now able to perform basic Racket programming on Windows.

By using the Racket REPL, you can execute Racket programs from the command line and enjoy interactive programming.
Next, let's actually try programming in Racket.

Happy Hacking!

Technical Terms and Notes

This section explains the technical terms used in this article:

  • Racket:
    A functional programming language based on Scheme, suitable for education, research, and experimental projects.

  • REPL (Read-Eval-Print-Loop):
    An environment that enables interactive programming where you can enter code from the command line and get immediate results.

  • Functional Programming:
    A programming paradigm built around functions, characterized by data immutability and minimization of side effects.

  • DrRacket:
    The official Racket Integrated Development Environment (IDE) where you can learn everything from programming basics to advanced techniques.

  • raco:
    A command-line tool that supports Racket package management, project building, and more.

  • XDG Base Directory:
    A standard directory specification in UNIX/Linux that defines locations for saving configuration and data files to facilitate system organization and management.

  • Macro System:
    A system where compilation performs code generation or syntax expansion, enhancing the flexibility and functionality of the programming language.

  • winget:
    The official package manager for Windows that allows for installing and managing applications from the command line.

  • Functional Programming Language:
    A language that treats functions as first-class objects and realizes a programming style with immutability and few side effects.

  • Macro System:
    In Racket, a feature that allows developers to extend the language's syntax and create new constructs.

  • XDG Base Directory:
    A standard directory structure used in UNIX/Linux systems for organizing and storing user configuration and data files. On Windows, the XDG environment variables are set to perform management according to this standard.

  • Dr Racket:
    An integrated development environment dedicated to the Racket programming language. It allows for editing, running, and debugging code within a single application, supporting everyone from programming learners to professionals.

  • raco:
    A command-line tool for Racket that supports various development-related tasks, such as package management, project building, and program execution.

  • winget:
    The official package manager for Windows. It is a tool that allows you to install software directly from the command line.

Reference Materials

Websites

Books

  • Racket Guide:
    A guidebook for beginners that explains Racket from basics to applications in an easy-to-understand way.
  • How to Design Programs:
    A textbook where you can step-by-step learn program design techniques centered on functional programming, from beginner to advanced levels.
  • Structure and Interpretation of Computer Programs:
    A classic text that provides a deep understanding of computer science and programming principles. Helpful for improving logical thinking and programming skills.
  • Beautiful Racket
    A practical guide to designing and implementing your own programming languages using Racket. Both beginners and intermediate users can learn how to create programming languages step-by-step.
GitHubで編集を提案

Discussion