Skip to content

Comment on Donald E. Knuth's Homepageparent

Comments

This is the original Knuth-style "literate programming", and being able to do this is approximately the entire point of CWEB.

BTW, note that the .w source file is not meant to be read directly: it is intended to be run through cweave (which generates a .tex file), and then typeset. In this case, I have a (not very up-to-date) repo at https://github.com/shreevatsa/knuth-literate-programs of the literate programs from Knuth's website, and othello.pdf is here: https://shreevatsa.github.io/knuth-literate-programs/program...

You're referring to section 4 of the program, which occurs at the bottom of page 2 here: https://shreevatsa.github.io/knuth-literate-programs/program... — in the typeset version, the text "If direction k allows a move, return 1; otherwise continue" is followed by the number 5, which means that the contents are in section 5 of the program. If you go to section 5 which is at the top of the next page (if viewing the PDF on a screen, you can simply click on the 5), you'll see the contents of the section (followed by "This code is used in section 4").

(It's also possible to define some code across multiple sections, e.g. the "<Subroutines 3>;" referenced in section 1 is defined across sections 3, 4, 6. All these conventions may seem unnecessary for a small program but they were devised for the medium-length (~20k lines) Pascal program TeX: https://mirrors.ctan.org/info/knuth-pdf/tex/tex.pdf and it's how Knuth now finds it most comfortable to program for programs of any length.)

(How does CWEB process this: ctangle replaces the text with the body of the loop; you could think of the entire `If direction |k| allows a move, |return 1|; otherwise |continue|` as a parameter-less macro with a long name, which is replaced by the preprocessor.)

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.