Skip to main content
Logo image

Derivatives and Integrals An Annotated Discourse

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.