Pascal, 1976 · JavaScript, 2026
Type a function. A fifty-year-old program builds it into a tree and climbs back out with the derivative.
I wrote this program in Pascal in 1976, for a computer science class. It reads a function, builds it in memory as a binary tree, walks the tree applying the rules of differentiation one node at a time, and prints the derivative. I was one of the few students to hand it in on time — on the last day of the semester — and it worked on every function I tried.
Its method is the same one Donald Knuth sets out as Algorithm D in The Art of Computer Programming, Volume 1 (§2.3.2): right-threaded trees, a postorder walk that finishes every child before its parent, and a tree-building routine that simplifies as it goes, quietly dropping ×1 and +0.
Fifty years later I typed the program in from the original printout, and it wouldn’t run. In November 2026 the culprit turned up: one mistyped letter in my transcription — llink where the original said rlink — plus a one-line setting that today’s compilers need before they’ll accept 1970s-standard Pascal. With those fixed, it ran correctly again.
This page is a line-by-line JavaScript port: the same tree, the same procedures (createtree, difftree, hangbinop, copy) and the same rules. The original read its function in prefix notation, one symbol at a time, and printed its answer the same way; here you type the function normally and the answer is typeset. A few rules were added in 2026 — ex, square roots, and any number (the original knew only 0, 1 and 2) — and they’re marked as such.
Your function
Its derivative
Raw output is exactly the tree the 1976 program built. It never did arithmetic on constants, so 2·x2−1 stays as it is and −sin x appears as 0 − sin x.
The program builds a tree from your function, then visits it in postorder — every child before its parent — so each node’s derivative is assembled from derivatives it has already found.
Each node in the tree is handled by one entry in the program’s rule table. Rules marked 1976 are in the original program; rules marked 2026 were added for this page. Here u and v stand for whatever sits below the node, and u′, v′ for their derivatives, already found.
Constants: in 1976, the letters a, b, c and the numbers 0, 1, 2. In 2026, any number (decimals and fractions too), any other letter, e and π. Any power, including xx, goes through the 1976 general power rule.