TPK algorithm: Difference between revisions

Content deleted Content added
on second thought, move the ALGOL 60 implementation with the mention of the other languages in the apper
Ricvelozo (talk | contribs)
 
(11 intermediate revisions by 4 users not shown)
Line 1:
{{Short description|Program to compare computer programming languages}}
The '''TPK algorithm''' is a simple [[computer program|program]] introduced by [[Donald Knuth]] and [[Luis Trabb Pardo]] to illustrate the evolution of computer [[programming language]]s. In their 1977 work "The Early Development of Programming Languages", Trabb Pardo and Knuth introduced a small program that involved [[Array data structure|arrays]], indexing, mathematical [[Function (mathematics)|function]]s, [[subroutine]]s, [[I/O]], [[conditional (programming)|conditional]]s and [[iteration]]. They then wrote implementations of the algorithm in several early programming languages to show how such concepts were expressed.
 
Line 88 ⟶ 89:
<syntaxhighlight lang="python" line>
from math import sqrt
 
 
def f(t):
return sqrt(abs(t)) + 5 * t ** 3
 
 
a = [float(input()) for _ in range(11)]
for i, t in reversed(list(enumerate(a))):
y = f(t)
print(i, "TOO LARGE" if y > 400: else y)
print(i, "TOO LARGE")
else:
print(i, y)
</syntaxhighlight>
 
Line 105:
 
<syntaxhighlight lang="Rust" line>
use std::io::{selfio, prelude::*iter};
 
fn f(t: f64) -> Option<f64> {
let y = t.abs().sqrt() + 5.0 * t.powi(3);
(y <= 400.0).then_some(y)
}
 
fn main() {
let mut a = [0f64; 11];
for (t, input) in a.iter_mut().iter::zip(&mut a, io::stdin().lock().lines()) {
*t = input.unwrap().parse().unwrap();
}
 
a.iter().enumerate().rev().for_each(|(i, &t)| match f(t) {
y if y > 400.0None => println!("{i} TOO LARGE", i),
Some(y) => println!("{i} {y}", i, y),
});
}
Line 127 ⟶ 128:
{{reflist|refs=
<ref name="edpl">Luis Trabb Pardo and Donald E. Knuth, "The Early Development of Programming Languages".
* First published August 1976 in [https://web.archive.org/web/20131102050629/http://www.textfiles.com/bitsavers/pdf/stanford/cs_techReports/STAN-CS-76-562_EarlyDevelPgmgLang_Aug76.pdf typewritten draft form, as Stanford CS Report STAN-CS-76-562]
* Published in ''Encyclopedia of Computer Science and Technology'', Jack Belzer, Albert G. Holzman, and [[Allen Kent]] (eds.), Vol. 6, pp. 419-493. Dekker, New York, 1977.
* Reprinted ({{doi|10.1016/B978-0-12-491650-0.50019-8}}) in ''A History of Computing in the Twentieth Century'', [[N. Metropolis]], [[Jack Howlett|J. Howlett]], and [[G.-C. Rota]] (eds.), New York, Academic Press, 1980. {{ISBN|0-12-491650-3}}