Skip to content

ruliana/racket-examples

Repository files navigation

Racket Examples

A collection of Racket examples exploring macros, reader macros, module system customization, and advanced control flow. Each example is self-contained and demonstrates a specific concept or technique.

Examples

Macros

File Description
simple-macro.rkt Basic macro with define-syntax-rule: a given binding form that sequences variable bindings
flexible-macro.rkt given macro using syntax-parse with support for single bindings, values destructuring, match-define, and struct patterns
macro-goes-frenzy.rkt given variant with = as separator: given a = 1 b = 2 ...
threading-macro.rkt Data pipeline composition using ~> and ~>> from the threading library

Parameters and Resource Management

File Description
parameters.rkt Dynamic parameters with make-parameter and parameterize for thread-local mutable state
custodians.rkt Custodian-based resource cleanup: demonstrates the problem of leaked resources and how custodians with dynamic-wind solve it

Advanced Control Flow

File Description
amb-operator.rkt The AMB (ambiguous choice) operator from SICP, implemented with continuations. Includes a constraint-satisfaction example that finds consecutive words sharing a letter

Reader Macros

File Description
reader-macro/reader-macro.rkt Custom readtable that adds /pattern/ as a regexp literal (shorthand for #px"pattern")
reader-macro/reader-macro-glue.rkt Module reader that wires the custom readtable into a #lang
reader-macro/reader-macro-use.rkt Usage example: /hello/ vs #px"hello"

Module System

Basic

File Description
module-basic/hate-define.rkt Language module that hides define and exposes it as def using except-out and rename-out
module-basic/hate-define-use.rkt Uses the hate-define module as a base language via #lang s-exp
module-basic/hate-lambda.rkt Same pattern applied to lambda
module-basic/hate-lambda-use.rkt Uses hate-lambda as a base language

Advanced

File Description
module-advanced/app-tricks.rkt Overrides #%app so every function call prints the function being applied
module-advanced/app-tricks-use.rkt Demonstrates the instrumented application operator
module-advanced/app-tricks2.rkt Overrides #%app to treat hash maps as callable — (my-hash 'key) performs a lookup
module-advanced/app-tricks2-use.rkt Uses the dict-callable syntax

Embedded Languages

File Description
algol60-example.rkt A working ALGOL 60 program inside Racket (#lang algol60), computing a summation via SIGMA
profj-example.rkt Java class syntax embedded in Racket via ProfJ

Themes

These examples collectively demonstrate Racket's core philosophy: language design is a programming technique, not a one-time task.

  • Macros — from trivial define-syntax-rule to complex multi-pattern syntax-parse
  • Reader extension — adding new literal syntax before the parser even sees the code
  • Module system — composing, restricting, and redefining language primitives
  • Operator customization — replacing core forms like #%app to change evaluation semantics
  • Continuationslet/cc for backtracking (AMB) and structured cleanup (custodians)
  • Embedded DSLs — running entirely different languages (ALGOL 60, Java) within a Racket file

Running the Examples

You need Racket installed. Most files can be run directly:

racket simple-macro.rkt
racket flexible-macro.rkt
racket amb-operator.rkt

The threading library is required by threading-macro.rkt:

raco pkg install threading

About

Examples of the nice parts of Racket

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages