You're right to ask! Emacs Lisp has a very mature and well-integrated literate programming system with org-mode. Scheme doesn't have quite the same level of built-in tooling, but there are several good options, ranging from simple to more sophisticated. Here's a breakdown of the common approaches, from easiest to more complex, with their pros and cons:
1. Simple Markdown/Text with Code Blocks (Easiest)
- How it works: Write your documentation in Markdown (or plain text) and use code blocks to embed Scheme code. You then have a separate process to extract and evaluate the code.
- Tools:
- Markdown editors: Any Markdown editor will work.
scheme interpreter: You'll need a Scheme interpreter (Racket, Guile, Chicken, etc.).
grep/sed/awk or a simple script: To extract the code blocks. You can write a small script to find lines starting with backticks (`scheme) and pass them to the Scheme interpreter.
- Example:
# My Awesome Scheme Program
This program calculates the factorial of a number.
```scheme
(define (factorial n)
(if (= n 0)
1
(* n (factorial (- n 1)))))
Let's test it:
(factorial 5)
This is a simple example, but it can be extended.
* **Pros:**
* Very easy to get started. No special tools needed beyond a text editor and a Scheme interpreter.
* Markdown is widely supported and readable.
* Portable.
* **Cons:**
* Manual extraction of code blocks. Can be tedious for larger documents.
* No automatic execution or weaving of results back into the document.
* Limited support for cross-references or complex document structure.
**2. Noweb-style Literate Programming (Classic Approach)**
* **How it works:** Noweb is a classic literate programming system. You write your code and documentation in a single file, using special sectioning markers (`<<...>>`). A preprocessor (`noweb`) extracts the code and documentation into separate files.
* **Tools:**
* **Noweb:** The original Noweb program (available for many platforms). Often packaged with Scheme distributions.
* **Scheme interpreter:** To run the extracted code.
* **Example:**
```noweb
\section{Factorial Function}
This function calculates the factorial of a non-negative integer.
<<factorial>>
(define (factorial n)
(if (= n 0)
1
(* n (factorial (- n 1)))))
\endsection
\section{Testing the Function}
Let's test the factorial function with a few examples.
<<test-factorial>>
(display (factorial 5)) (newline)
(display (factorial 0)) (newline)
\endsection
You would then run noweb myprogram.nw to generate myprogram.scm (the code) and myprogram.txt (the documentation).
- Pros:
- Well-established and widely understood.
- Clear separation of code and documentation.
- Good for larger projects.
- Cons:
- Noweb syntax can be a bit quirky.
- Requires a separate preprocessor step.
- Less integrated with modern editors.
3. Racket's Literate Programming Support (Best for Racket Users)
- How it works: Racket has built-in support for literate programming using
#lang racket/literate. You write your code and documentation in a single Racket file, using special syntax for code blocks and documentation. Racket's raco tool can then weave the document into a single file or extract the code.
- Tools:
- Racket: The Racket programming environment.
raco weave: Racket's weaving tool.
- Example:
#lang racket/literate
;;; This is a comment that will appear in the documentation.
(define (factorial n)
(if (= n 0)
1
(* n (factorial (- n 1)))))
;;; Let's test the function.
(factorial 5)
You would then run raco weave myprogram.rkt to generate a woven document (e.g., myprogram.html or myprogram.pdf).
- Pros:
- Excellent integration with Racket.
- Easy to use.
- Supports weaving into various formats (HTML, PDF, etc.).
- Racket's documentation system is very powerful.
- Cons:
- Tied to the Racket ecosystem. Not portable to other Scheme implementations without modification.
4. Other Options (Less Common)
- Scribble (for Racket): Scribble is a documentation system for Racket that can also be used for literate programming. It's more focused on generating high-quality documentation than on weaving code and documentation together.
- Custom Scripts: You can write your own scripts (in Scheme or another language) to parse a custom literate programming format and extract/evaluate the code. This gives you the most flexibility but requires the most effort.
Which one should you choose?
- For quick and simple projects: Markdown with code blocks is a good starting point.
- If you're already using Racket: Racket's
#lang racket/literate is the best option. It's well-integrated, easy to use, and produces high-quality results.
- If you want a classic, portable approach: Noweb is a solid choice.
- If you need a highly customized solution: Consider writing your own scripts.
Resources:
In summary, while Scheme doesn't have a single dominant literate programming solution like Emacs Lisp's org-mode, there are several viable options. Racket's built-in support is the most convenient if you're using Racket, while Markdown and Noweb offer more general-purpose approaches. Choose the one that best fits your needs and workflow.