(* -------------------------------------------------------------------- This program defines and evaluates a polynomial -- with integer coefficients and an integer solution, of course -- using Horner's Rule. The main program's argument is the value at which to evaluate the polynomial. The polynomial's coefficients are defined using an array indexed 0 through n, where n is the degree of the polynomial and coefficient[i] is the coefficient on the ith-power term. The array is simulated by a function, which gives one example of how to represent more complex data types using Klein's built in types of boolean, integer, and function. Based on code written by Doug Baldwin for his MinimL language. -------------------------------------------------------------------- *) (* -------------------------------------------------------------------- This main hard-codes the degree of the polynomial with the second arg it passes to horner, and by the selection in coefficients. -------------------------------------------------------------------- *) function main( x : integer ) : integer horner( x, 3, 0 ) (* before *) function horner(x: integer, n: integer, value: integer): integer if GE(n, 0) then horner( x, n - 1, (value * x) + coefficient(n) ) else value (* after *) function horner(x: integer, n: integer, value: integer): integer if n < 0 then value else horner( x, n - 1, (value * x) + coefficient(n) ) (* -------------------------------------------------------------------- This function defines the polynomial x^3 - 4x^2 + 2x + 9. -------------------------------------------------------------------- *) function coefficient( i : integer ) : integer if i < 1 then 9 else if i < 2 then 2 else if i < 3 then -4 else 1