在本页中:
Racket 指南
7.0.0.18

Racket 指南

Matthew Flatt,
Robert Bruce Findler,
PLT,
以及Racket-zh 项目组译

本指南面向尚无 Racket 经验或有部分经验的程序员,即假定读者有编程经验。 如果你是编程新手,请考虑阅读 How to Design Programs。如果你想要对 Racket 有一个大概的了解, 请参阅 Quick: An Introduction to Racket with Pictures

本指南的第二章对 Racket 进行了简单的介绍。 从第三章开始,本指南将深入细节—其中涵盖了 Racket 的大部分工具箱,不过精确的细节还请参阅The Racket Reference和其它参考手册。

本文档的源代码可从 GitHub 上获取。

    1 欢迎来到 Racket

      1.1 与 Racket 进行交互

      1.2 定义与交互

      1.3 创建可执行文件

      1.4 Lisp/Scheme 经验者注意

    2 Racket 精要

      2.1 简单的值

      2.2 简单的定义与表达式

        2.2.1 定义

        2.2.2 关于代码缩进的提示

        2.2.3 标识符

        2.2.4 函数调用(过程应用)

        2.2.5 条件分支与 ifandorcond

        2.2.6 再谈函数调用

        2.2.7 匿名函数与 lambda

        2.2.8 局部绑定与 defineletlet*

      2.3 列表,迭代与递归

        2.3.1 预定义的列表循环

        2.3.2 从零开始构造列表迭代

        2.3.3 尾递归

        2.3.4 递归与迭代

      2.4 序对、列表和 Racket 语法

        2.4.1 quote引述序对和符号

        2.4.2 quote 简写为 '

        2.4.3 列表与 Racket 语法

    3 内建数据类型

      3.1 布尔值

      3.2 数值

      3.3 字符

      3.4 字符串(Unicode)

      3.5 字节与字节串

      3.6 符号

      3.7 关键字

      3.8 序对与列表

      3.9 向量

      3.10 散列表

      3.11 盒子

      3.12 void 与 undefined

    4 表达式与定义

      4.1 记法

      4.2 标识符与绑定

      4.3 函数调用(过程应用)

        4.3.1 求值顺序与参数量

        4.3.2 关键字参数

        4.3.3 apply 函数

      4.4 函数(过程):lambda

        4.4.1 声明剩余参数

        4.4.2 声明可选参数

        4.4.3 声明关键字参数

        4.4.4 参数量敏感的函数:case-lambda

      4.5 定义:define

        4.5.1 函数简写

        4.5.2 柯里化函数简写

        4.5.3 多值与 define-values

        4.5.4 内部定义

      4.6 局部绑定

        4.6.1 平行绑定:let

        4.6.2 顺序绑定:let*

        4.6.3 递归绑定:letrec

        4.6.4 命名的 let

        4.6.5 多值绑定:let-valueslet*-valuesletrec-values

      4.7 条件分支

        4.7.1 简单分支:if

        4.7.2 组合测试:andor

        4.7.3 链式测试:cond

      4.8 序列

        4.8.1 作用在前:begin

        4.8.2 作用在后:begin0

        4.8.3 按条件作用:whenunless

      4.9 赋值:set!

        4.9.1 赋值使用准则

        4.9.2 多重赋值:set!-values

      4.10 引述:quote'

      4.11 准引述:quasiquote

      4.12 简单分派:case

      4.13 动态绑定:parameterize

    5 Programmer-Defined Datatypes

      5.1 Simple Structure Types: struct

      5.2 Copying and Update

      5.3 Structure Subtypes

      5.4 Opaque versus Transparent Structure Types

      5.5 Structure Comparisons

      5.6 Structure Type Generativity

      5.7 Prefab Structure Types

      5.8 More Structure Type Options

    6 Modules

      6.1 Module Basics

        6.1.1 Organizing Modules

        6.1.2 Library Collections

        6.1.3 Packages and Collections

        6.1.4 Adding Collections

      6.2 Module Syntax

        6.2.1 The module Form

        6.2.2 The #lang Shorthand

        6.2.3 Submodules

        6.2.4 Main and Test Submodules

      6.3 Module Paths

      6.4 Imports: require

      6.5 Exports: provide

      6.6 Assignment and Redefinition

      6.7 Modules and Macros

    7 Contracts

      7.1 Contracts and Boundaries

        7.1.1 Contract Violations

        7.1.2 Experimenting with Contracts and Modules

        7.1.3 Experimenting with Nested Contract Boundaries

      7.2 Simple Contracts on Functions

        7.2.1 Styles of ->

        7.2.2 Using define/contract and ->

        7.2.3 any and any/c

        7.2.4 Rolling Your Own Contracts

        7.2.5 Contracts on Higher-order Functions

        7.2.6 Contract Messages with “???”

        7.2.7 Dissecting a contract error message

      7.3 Contracts on Functions in General

        7.3.1 Optional Arguments

        7.3.2 Rest Arguments

        7.3.3 Keyword Arguments

        7.3.4 Optional Keyword Arguments

        7.3.5 Contracts for case-lambda

        7.3.6 Argument and Result Dependencies

        7.3.7 Checking State Changes

        7.3.8 Multiple Result Values

        7.3.9 Fixed but Statically Unknown Arities

      7.4 Contracts: A Thorough Example

      7.5 Contracts on Structures

        7.5.1 Guarantees for a Specific Value

        7.5.2 Guarantees for All Values

        7.5.3 Checking Properties of Data Structures

      7.6 Abstract Contracts using #:exists and #:∃

      7.7 Additional Examples

        7.7.1 A Customer-Manager Component

        7.7.2 A Parameteric (Simple) Stack

        7.7.3 A Dictionary

        7.7.4 A Queue

      7.8 Building New Contracts

        7.8.1 Contract Struct Properties

        7.8.2 With all the Bells and Whistles

      7.9 Gotchas

        7.9.1 Contracts and eq?

        7.9.2 Contract boundaries and define/contract

        7.9.3 Exists Contracts and Predicates

        7.9.4 Defining Recursive Contracts

        7.9.5 Mixing set! and contract-out

    8 Input and Output

      8.1 Varieties of Ports

      8.2 Default Ports

      8.3 Reading and Writing Racket Data

      8.4 Datatypes and Serialization

      8.5 Bytes, Characters, and Encodings

      8.6 I/O Patterns

    9 Regular Expressions

      9.1 Writing Regexp Patterns

      9.2 Matching Regexp Patterns

      9.3 Basic Assertions

      9.4 Characters and Character Classes

        9.4.1 Some Frequently Used Character Classes

        9.4.2 POSIX character classes

      9.5 Quantifiers

      9.6 Clusters

        9.6.1 Backreferences

        9.6.2 Non-capturing Clusters

        9.6.3 Cloisters

      9.7 Alternation

      9.8 Backtracking

      9.9 Looking Ahead and Behind

        9.9.1 Lookahead

        9.9.2 Lookbehind

      9.10 An Extended Example

    10 Exceptions and Control

      10.1 Exceptions

      10.2 Prompts and Aborts

      10.3 Continuations

    11 Iterations and Comprehensions

      11.1 Sequence Constructors

      11.2 for and for*

      11.3 for/list and for*/list

      11.4 for/vector and for*/vector

      11.5 for/and and for/or

      11.6 for/first and for/last

      11.7 for/fold and for*/fold

      11.8 Multiple-Valued Sequences

      11.9 Breaking an Iteration

      11.10 Iteration Performance

    12 Pattern Matching

    13 Classes and Objects

      13.1 Methods

      13.2 Initialization Arguments

      13.3 Internal and External Names

      13.4 Interfaces

      13.5 Final, Augment, and Inner

      13.6 Controlling the Scope of External Names

      13.7 Mixins

        13.7.1 Mixins and Interfaces

        13.7.2 The mixin Form

        13.7.3 Parameterized Mixins

      13.8 Traits

        13.8.1 Traits as Sets of Mixins

        13.8.2 Inherit and Super in Traits

        13.8.3 The trait Form

      13.9 Class Contracts

        13.9.1 External Class Contracts

        13.9.2 Internal Class Contracts

    14 Units (Components)

      14.1 Signatures and Units

      14.2 Invoking Units

      14.3 Linking Units

      14.4 First-Class Units

      14.5 Whole-module Signatures and Units

      14.6 Contracts for Units

        14.6.1 Adding Contracts to Signatures

        14.6.2 Adding Contracts to Units

      14.7 unit versus module

    15 Reflection and Dynamic Evaluation

      15.1 eval

        15.1.1 Local Scopes

        15.1.2 Namespaces

        15.1.3 Namespaces and Modules

      15.2 Manipulating Namespaces

        15.2.1 Creating and Installing Namespaces

        15.2.2 Sharing Data and Code Across Namespaces

      15.3 Scripting Evaluation and Using load

    16 Macros

      16.1 Pattern-Based Macros

        16.1.1 define-syntax-rule

        16.1.2 Lexical Scope

        16.1.3 define-syntax and syntax-rules

        16.1.4 Matching Sequences

        16.1.5 Identifier Macros

        16.1.6 set! Transformers

        16.1.7 Macro-Generating Macros

        16.1.8 Extended Example: Call-by-Reference Functions

      16.2 General Macro Transformers

        16.2.1 Syntax Objects

        16.2.2 Macro Transformer Procedures

        16.2.3 Mixing Patterns and Expressions: syntax-case

        16.2.4 with-syntax and generate-temporaries

        16.2.5 Compile and Run-Time Phases

        16.2.6 General Phase Levels

          16.2.6.1 Phases and Bindings

          16.2.6.2 Phases and Modules

        16.2.7 Syntax Taints

      16.3 Module Instantiations and Visits

        16.3.1 Declaration versus Instantiation

        16.3.2 Compile-Time Instantiation

        16.3.3 Visiting Modules

        16.3.4 Lazy Visits via Available Modules

    17 Creating Languages

      17.1 Module Languages

        17.1.1 Implicit Form Bindings

        17.1.2 Using #lang s-exp

      17.2 Reader Extensions

        17.2.1 Source Locations

        17.2.2 Readtables

      17.3 Defining new #lang Languages

        17.3.1 Designating a #lang Language

        17.3.2 Using #lang reader

        17.3.3 Using #lang s-exp syntax/module-reader

        17.3.4 Installing a Language

        17.3.5 Source-Handling Configuration

        17.3.6 Module-Handling Configuration

    18 Concurrency and Synchronization

      18.1 Threads

      18.2 Thread Mailboxes

      18.3 Semaphores

      18.4 Channels

      18.5 Buffered Asynchronous Channels

      18.6 Synchronizable Events and sync

      18.7 Building Your Own Synchronization Patterns

    19 Performance

      19.1 Performance in DrRacket

      19.2 The Bytecode and Just-in-Time (JIT) Compilers

      19.3 Modules and Performance

      19.4 Function-Call Optimizations

      19.5 Mutation and Performance

      19.6 letrec Performance

      19.7 Fixnum and Flonum Optimizations

      19.8 Unchecked, Unsafe Operations

      19.9 Foreign Pointers

      19.10 Regular Expression Performance

      19.11 Memory Management

      19.12 Reachability and Garbage Collection

      19.13 Weak Boxes and Testing

      19.14 Reducing Garbage Collection Pauses

    20 Parallelism

      20.1 Parallelism with Futures

      20.2 Parallelism with Places

      20.3 Distributed Places

    21 Running and Creating Executables

      21.1 Running racket and gracket

        21.1.1 Interactive Mode

        21.1.2 Module Mode

        21.1.3 Load Mode

      21.2 Scripts

        21.2.1 Unix Scripts

        21.2.2 Windows Batch Files

      21.3 Creating Stand-Alone Executables

    22 More Libraries

      22.1 Graphics and GUIs

      22.2 The Web Server

      22.3 Using Foreign Libraries

      22.4 And More

    23 Dialects of Racket and Scheme

      23.1 More Rackets

      23.2 Standards

        23.2.1 R5RS

        23.2.2 R6RS

      23.3 Teaching

    24 Command-Line Tools and Your Editor of Choice

      24.1 Command-Line Tools

        24.1.1 Compilation and Configuration: raco

        24.1.2 Interactive evaluation

        24.1.3 Shell completion

      24.2 Emacs

        24.2.1 Major Modes

        24.2.2 Minor Modes

        24.2.3 Packages specific to Evil Mode

      24.3 Vim

      24.4 Sublime Text

    Bibliography

    Index