Trabb Pardo-Knuth algorithm

Trabb Pardo-Knuth algorithm

The Trabb Pardo-Knuth algorithm is a program introduced by Donald Knuth and Luis Trabb Pardo to illustrate the evolution of computer programming languages.

In their 1977 work "The Early Development of Programming Languages", Trabb Pardo and Knuth introduced a trivial program which involved arrays, indexing, mathematical functions, subroutines, I/O, conditionals and iteration. They then wrote implementations of the algorithm in several early programming languages to show how such concepts were expressed.

The simpler Hello world program has been used for much the same purpose.

The algorithm

ask for 11 numbers into sequence S reverse sequence S for each item in sequence S do an operation if result overflows alert user else print result

The algorithm reads eleven numbers from an input device, stores them in an array, and then processes them in reverse order, applying a user-defined function to each value and reporting either the value of the function or a message to the effect that the value has exceeded some threshold.

Implementations

ALGOL 60

begin integer i; real y; real array a [0:10] ; real procedure f(t); real t; value t; f := sqrt(abs(t))+5*t^3; for i := 0 step 1 until 10 do read(a [i] ); for i := 10 step -1 until 0 do begin y := f(a [i] ); if y > 400 then write(i, "TOO LARGE") else write(i,y); end end

The problem with the usually specified function is that the term 5*t^3 gives overflows in almost all languages for very large negative values.

Ruby version

The Ruby version takes advantage of some of its features:

class TPK def f( x ) return Math.sqrt(x.abs) + 5*x **3 end

def main Array.new(11) { gets.to_i }.reverse.each do |x
y = f(x) puts "#{x} #{(y>400) ? 'TOO LARGE' : y}" end endend

Ruby handles numerical overflow by returning Infinity, which is greater than 400.

Ocaml version

The Ocaml version using imperative features such as for loops:

let tpk () = let f x = sqrt x +. 5.0 *. (x ** 3.0) in let pr f = print_endline (if f > 400.0 then "overflow" else string_of_float f) in let read () = float_of_string (read_line ()) in let a = Array.init 11 (fun _ -> read ()) in for i = 10 downto 0 do pr (f a.(i)) done

A functional version can also be written in Ocaml:

let tpk l = let f x = sqrt x +. 5.0 *. (x ** 3.0) in let p x = x < 400.0 in List.filter p (List.map f (List.rev l))

References

* "The Early Development of Programming Languages" in "A History of Computing in the Twentieth Century", New York, Academic Press, 1980. ISBN 0-12-491650-3 (Reprinted in Knuth, Donald E., "et al", "Selected Papers on Computer Languages", Stanford, CA, CSLI, 2003. ISBN 1-57586-382-0)

External links

* [http://cs.fit.edu/~ryan/compare Implementations in several modern languages]


Wikimedia Foundation. 2010.

Игры ⚽ Нужна курсовая?

Look at other dictionaries:

  • Knuth–Morris–Pratt algorithm — The Knuth–Morris–Pratt string searching algorithm (or KMP algorithm) searches for occurrences of a word W within a main text string S by employing the observation that when a mismatch occurs, the word itself embodies sufficient information to… …   Wikipedia

  • Knuth Prize — Gary Miller presents Volker Strassen with the 2008 Knuth Prize at SODA 2009. The Donald E. Knuth Prize is a prize for outstanding contributions to the foundations of computer science, named after Donald E. Knuth. Contents …   Wikipedia

  • Donald Knuth — Donald Ervin Knuth Donald Knuth at a reception for the Open Content Alliance, October 25, 2005 Born …   Wikipedia

  • Dancing Links — In computer science, Dancing Links, also known as DLX, is the technique suggested by Donald Knuth to efficiently implement his Algorithm X.[1] Algorithm X is a recursive, nondeterministic, depth first, backtracking algorithm that finds all… …   Wikipedia

  • Hello world program — Hello World redirects here. For the 2009 compilation album by Michael Jackson, see Hello World: The Motown Solo Collection. For the song by Lady Antebellum, see Hello World (song). A GUI Hello World program, written in Perl …   Wikipedia

  • Кнут, Дональд Эрвин — В Википедии есть статьи о других людях с такой фамилией, см. Кнут. Дональд Эрвин Кнут Donald Ervin Knuth …   Википедия

  • Metafont — Developer(s) Donald Knuth Stable release 2.718281 / March 2008 Operating syste …   Wikipedia

  • Concrete Mathematics — Concrete Mathematics: A Foundation for Computer Science   …   Wikipedia

  • Computers and Typesetting — is a 5 volume set of books by Donald Knuth published 1986 describing the TeX and Metafont systems for digital typography. Knuth s computers and typesetting project was the result of his frustration with the lack of decent software for the… …   Wikipedia

  • Computer Modern — Category Serif Classification Didone Designer(s) Donald Kn …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”