Trabb Pardo Knuth Algorithm

April 27, 2012

In their 1973 paper “The Early Development of Programming Languages,” Luis Trabb Pardo and Donald Knuth give the following algorithm as a means of assessing programming languages:

ask for 11 numbers to be read into a sequence S
reverse sequence S
for each item in sequence S
call a function to do an operation
if result overflows
alert user
else
print result

Though it is trivial, the algorithm involves arrays, indexing, mathematical functions, subroutines, I/O, conditionals and iteration, and thus exposes many basic operations of a programming language.

Your task is to write a program that implements the Trabb Pardo Knuth algorithm. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

‘input’ in Python3 is the same as ‘raw_input’ in Python2, i.e. it doesn’t eval the input, but just returns it as a string. This means I cast to float before passing to my function ‘sqrt’. I broaden the scope of error catching from OverflowError to all ValueErrors, which includes failure to convert the entered string into a float.

Remco, I think a Haskell solution truer to the spirit of the exercise would probably use something like GHC’s exception system to handle numeric overflow. Yours forbids it, rather than actually dealing with it.