Skip to main content

Section 30 Literate Programming

There is support for literate programming using the <fragment> tag. It should always contain an @@xml:id or a @@filename attribute, and may contain <code> segments as well as <fragref> segments referencing other fragments. The final code is to be assembled starting from a fragment with a filename attribute and traversing the tree of fragment references.
For example here is the gcd algorithm in Python. Note that indentation is hard to get right at the moment.

⟨1 The GCD algorithm⟩ ≡

Root of file: gcd.py
def gcd(a, b):
    while(b):
⟨The key loop part 2⟩
return a
The key part is the inner part of the loop:

⟨2 The key loop part⟩ ≡

a, b = b, a % b
This double assignment changes both a and b.