Horner's rule for polynomial evaluation: Difference between revisions

Added Easylang
(Added Easylang)
 
(43 intermediate revisions by 27 users not shown)
Line 20:
 
Cf. [[Formal power series]]
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F horner(coeffs, x)
V acc = 0
L(c) reversed(coeffs)
acc = acc * x + c
R acc
 
print(horner([-19, 7, -4, 6], 3))</syntaxhighlight>
 
{{out}}
<pre>
128
</pre>
 
=={{header|360 Assembly}}==
<langsyntaxhighlight lang="360asm">* Horner's rule for polynomial evaluation - 07/10/2015
HORNER CSECT
USING HORNER,R15 set base register
Line 42 ⟶ 58:
PG DS CL12 buffer
YREGS
END HORNER</langsyntaxhighlight>
{{out}}
<pre>
Line 49 ⟶ 65:
 
=={{header|ACL2}}==
<langsyntaxhighlight Lisplang="lisp">(defun horner (ps x)
(if (endp ps)
0
(+ (first ps)
(* x (horner (rest ps) x)))))</langsyntaxhighlight>
 
=={{header|Action!}}==
<syntaxhighlight lang="action!">INT FUNC Horner(INT ARRAY coeffs INT count,x)
INT v,i
 
v=0 i=count-1
WHILE i>=0
DO
v=v*x+coeffs(i)
i==-1
OD
RETURN (v)
 
PROC Main()
INT ARRAY coeffs=[65517 7 65532 6]
INT res,x=[3],i,count=[4]
 
PrintF("x=%I%E",x)
FOR i=0 TO count-1
DO
PrintI(coeffs(i))
IF i=1 THEN
Print("x")
ELSEIF i>1 THEN
PrintF("x^%I",i)
FI
IF i<count-1 AND coeffs(i+1)>=0 THEN
Print("+")
FI
OD
res=Horner(coeffs,4,x)
PrintF("=%I%E",res)
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Horner's_rule_for_polynomial_evaluation.png Screenshot from Atari 8-bit computer]
<pre>
x=3
-19+7x-4x^2+6x^3=128
</pre>
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Float_Text_IO; use Ada.Float_Text_IO;
 
procedure Horners_Rule is
Line 72 ⟶ 127:
begin
Put(Horner(Coeffs => (-19.0, 7.0, -4.0, 6.0), Val => 3.0), Aft=>1, Exp=>0);
end Horners_Rule;</langsyntaxhighlight>
Output:
<pre>128.0</pre>
 
=={{header|Aime}}==
<langsyntaxhighlight lang="aime">real
horner(list coeffs, real x)
{
Line 99 ⟶ 154:
 
0;
}</langsyntaxhighlight>
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G}}
<langsyntaxhighlight lang="algol68">PROC horner = ([]REAL c, REAL x)REAL :
(
REAL res := 0.0;
Line 115 ⟶ 170:
[4]REAL coeffs := (-19.0, 7.0, -4.0, 6.0);
print( horner(coeffs, 3.0) )
)</langsyntaxhighlight>
 
=={{header|ALGOL W}}==
<syntaxhighlight lang="algolw">begin
% Horner's rule for polynominal evaluation %
% returns the value of the polynominal defined by coefficients, %
% at the point x. The number of coefficients must be in ub %
% the coefficients should be in order with x^0 first, x^n last %
real procedure Horner( real array coefficients ( * )
; integer value ub
; real value x
) ;
begin
real xValue;
xValue := 0;
for i := ub step -1 until 1 do xValue := ( xValue * x ) + coefficients( i );
xValue
end Horner ;
% task test case %
begin
real array coefficients ( 1 :: 4 );
integer cPos;
cPos := 1;
for i := -19, 7, -4, 6 do begin
coefficients( cPos ) := i;
cPos := cPos + 1
end for_i ;
write( r_format := "A", r_w := 8, r_d := 2, Horner( coefficients, 4, 3 ) )
end test_cases
end.</syntaxhighlight>
{{out}}
<pre>
128.00
</pre>
 
=={{header|APL}}==
Works in [[Dyalog APL]]
<syntaxhighlight lang="apl">h←⊥∘⌽</syntaxhighlight>
{{output}}
<pre>
3 h ¯19 7 ¯4 6
128
</pre>
 
=={{header|ATS}}==
<langsyntaxhighlight ATSlang="ats">#include
"share/atspre_staload.hats"
 
Line 141 ⟶ 238:
in
println! (res)
end // end of [main0]</langsyntaxhighlight>
 
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">horner: function [coeffs, x][
result: 0
loop reverse coeffs 'c ->
result: c + result * x
return result
]
 
print horner @[neg 19, 7, neg 4, 6] 3</syntaxhighlight>
{{out}}
<pre>128</pre>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight lang="autohotkey">Coefficients = -19, 7, -4, 6
x := 3
 
Line 159 ⟶ 268:
i := Co0 - A_Index + 1, Result := Result * x + Co%i%
Return, Result
}</langsyntaxhighlight>
Message box shows:
<pre>128</pre>
 
=={{header|AWK}}==
<langsyntaxhighlight lang="awk">#!/usr/bin/awk -f
function horner(x, A) {
acc = 0;
Line 175 ⟶ 284:
split(p,P);
print horner(x,P);
}</langsyntaxhighlight>
 
{{out}}
Line 182 ⟶ 291:
128
</pre>
 
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
{{works with|Chipmunk Basic}}
{{works with|GW-BASIC}}
<syntaxhighlight lang="qbasic">100 HOME : REM 100 CLS for Chipmunk Basic and GW-BASIC
100 CLS : REM 100 HOME for Applesoft BASIC
110 X = 3
120 DIM COEFFS(3)
130 COEFFS(0) = -19
140 COEFFS(1) = 7
150 COEFFS(2) = -4
160 COEFFS(3) = 6
170 PRINT "Horner's algorithm for the polynomial "
180 PRINT "6*x^3 - 4*x^2 + 7*x - 19 when x = 3 is: ";
190 ACCUM = 0
200 FOR I = 3 TO 0 STEP -1
210 ACCUM = (ACCUM*X)+COEFFS(I)
220 NEXT I
230 PRINT ACCUM
240 END</syntaxhighlight>
 
==={{header|BASIC256}}===
<syntaxhighlight lang="vb">x = 3
dim coeficientes = {-19, 7, -4, 6}
print "Horner's algorithm for the polynomial ";
print "6*x^3 - 4*x^2 + 7*x - 19 when x = 3: ";
print AlgoritmoHorner(coeficientes, x)
end
 
function AlgoritmoHorner(coeffs, x)
acumulador = 0
for i = coeffs[?]-1 to 0 step -1
acumulador = (acumulador * x) + coeffs[i]
next i
return acumulador
end function</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">100 CLS
110 x = 3
120 DIM coeffs(3)
130 coeffs(0) = -19
140 coeffs(1) = 7
150 coeffs(2) = -4
160 coeffs(3) = 6
170 PRINT "Horner's algorithm for the polynomial "
180 PRINT "6*x^3 - 4*x^2 + 7*x - 19 when x = 3 is: ";
190 accum = 0
200 FOR i = UBOUND(coeffs,1) TO 0 STEP -1
210 accum = (accum*x)+coeffs(i)
220 NEXT i
230 PRINT accum
240 END</syntaxhighlight>
{{out}}
<pre>Horner's algorithm for the polynomial
6*x^3 - 4*x^2 + 7*x - 19 when x = 3 is: 128</pre>
 
==={{header|Gambas}}===
<syntaxhighlight lang="vbnet">Public coeficientes As New Integer[4]
 
Public Function AlgoritmoHorner(coeficientes As Integer[], x As Integer) As Integer
coeficientes[0] = -19
coeficientes[1] = 7
coeficientes[2] = -4
coeficientes[3] = 6
Dim i As Integer, acumulador As Integer = 0
 
For i = coeficientes.Count - 1 To 0 Step -1
acumulador = (acumulador * x) + coeficientes[i]
Next
Return acumulador
End Function
 
Public Sub Main()
Dim x As Integer = 3
Print "Horner's algorithm for the polynomial 6*x^3 - 4*x^2 + 7*x - 19 when x = 3: ";
Print AlgoritmoHorner(coeficientes, x)
End </syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|GW-BASIC}}===
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|PC-BASIC|any}}
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">100 CLS : REM 100 HOME for Applesoft BASIC
110 X = 3
120 DIM COEFFS(3)
130 COEFFS(0) = -19
140 COEFFS(1) = 7
150 COEFFS(2) = -4
160 COEFFS(3) = 6
170 PRINT "Horner's algorithm for the polynomial "
180 PRINT "6*x^3 - 4*x^2 + 7*x - 19 when x = 3 is: ";
190 ACCUM = 0
200 FOR I = 3 TO 0 STEP -1
210 ACCUM = (ACCUM*X)+COEFFS(I)
220 NEXT I
230 PRINT ACCUM
240 END</syntaxhighlight>
{{out}}
<pre>Same as Chipmunk Basic entry.</pre>
 
==={{header|Minimal BASIC}}===
{{works with|QBasic}}
{{works with|QuickBasic}}
{{works with|Applesoft BASIC}}
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|GW-BASIC}}
{{works with|MSX BASIC}}
{{works with|Just BASIC}}
{{works with|Liberty BASIC}}
{{works with|Run BASIC}}
{{works with|Yabasic}}
<syntaxhighlight lang="qbasic">20 LET X = 3
30 DIM C(3)
40 LET C(0) = -19
50 LET C(1) = 7
60 LET C(2) = -4
70 LET C(3) = 6
80 PRINT "HORNER'S ALGORITHM FOR THE POLYNOMIAL"
90 PRINT "6*X^3 - 4*X^2 + 7*X - 19 WHEN X = 3 : ";
100 LET A = 0
110 FOR I = 3 TO 0 STEP -1
120 LET A = (A*X)+C(I)
130 NEXT I
140 PRINT A
150 END</syntaxhighlight>
{{out}}
<pre>Same as Chipmunk Basic entry.</pre>
 
==={{header|MSX Basic}}===
The [[#Minimal_BASIC|Minimal BASIC]] solution works without any changes.
 
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">FUNCTION Horner (coeffs(), x)
acumulador = 0
FOR i = UBOUND(coeffs) TO LBOUND(coeffs) STEP -1
acumulador = (acumulador * x) + coeffs(i)
NEXT i
Horner = acumulador
END FUNCTION
 
x = 3
DIM coeffs(3)
DATA -19, 7, -4, 6
FOR a = LBOUND(coeffs) TO UBOUND(coeffs)
READ coeffs(a)
NEXT a
 
PRINT "Horner's algorithm for the polynomial 6*x^3 - 4*x^2 + 7*x - 19 when x = 3 is: ";
PRINT Horner(coeffs(), x)
END</syntaxhighlight>
 
==={{header|Quite BASIC}}===
The [[#Minimal_BASIC|Minimal BASIC]] solution works without any changes.
 
==={{header|Yabasic}}===
<syntaxhighlight lang="vb">x = 3
dim coeffs(4)
coeffs(0) = -19
coeffs(1) = 7
coeffs(2) = -4
coeffs(3) = 6
print "Horner's algorithm for the polynomial ";
print "6*x^3 - 4*x^2 + 7*x - 19 when x = 3: ";
print AlgoritmoHorner(coeffs, x)
end
 
sub AlgoritmoHorner(coeffs, x)
local acumulador, i
acumulador = 0
for i = arraysize(coeffs(),1) to 0 step -1
acumulador = (acumulador * x) + coeffs(i)
next i
return acumulador
end sub</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
=={{header|Batch File}}==
<langsyntaxhighlight lang="dos">
@echo off
 
Line 208 ⟶ 511:
echo %return%
exit /b
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 216 ⟶ 519:
128
</pre>
 
 
=={{header|BBC BASIC}}==
<langsyntaxhighlight lang="bbcbasic"> DIM coefficients(3)
coefficients() = -19, 7, -4, 6
PRINT FNhorner(coefficients(), 3)
Line 229 ⟶ 531:
v = v * x + coeffs(i%)
NEXT
= v</langsyntaxhighlight>
 
=={{header|Bracmat}}==
<langsyntaxhighlight lang="bracmat">( ( Horner
= accumulator coefficients x coeff
. !arg:(?coefficients.?x)
Line 243 ⟶ 545:
)
& Horner$(-19 7 -4 6.3)
);</langsyntaxhighlight>
Output:
<pre>128</pre>
Line 249 ⟶ 551:
=={{header|C}}==
{{trans|Fortran}}
<langsyntaxhighlight lang="c">#include <stdio.h>
 
double horner(double *coeffs, int s, double x)
Line 270 ⟶ 572:
printf("%5.1f\n", horner(coeffs, sizeof(coeffs)/sizeof(double), 3.0));
return 0;
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
using System.Linq;
 
Line 288 ⟶ 590:
Console.WriteLine(Horner(new[] { -19.0, 7.0, -4.0, 6.0 }, 3.0));
}
}</langsyntaxhighlight>
Output:
<pre>128</pre>
Line 295 ⟶ 597:
The same C function works too, but another solution could be:
 
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <vector>
 
Line 315 ⟶ 617:
cout << horner(v, 3.0) << endl;
return 0;
}</langsyntaxhighlight>
 
Yet another solution, which is more idiomatic in C++ and works on any bidirectional sequence:
 
<langsyntaxhighlight lang="cpp">
#include <iostream>
 
Line 336 ⟶ 638:
std::cout << horner(c, c + 4, 3) << std::endl;
}
</syntaxhighlight>
</lang>
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(defn horner [coeffs x]
(reduce #(-> %1 (* x) (+ %2)) (reverse coeffs)))
 
(println (horner [-19 7 -4 6] 3))</langsyntaxhighlight>
 
=={{header|CoffeeScript}}==
<langsyntaxhighlight lang="coffeescript">
eval_poly = (x, coefficients) ->
# coefficients are for ascending powers
Line 355 ⟶ 657:
console.log eval_poly 10, [4, 3, 2, 1] # 1234
console.log eval_poly 2, [1, 1, 0, 0, 1] # 19
</syntaxhighlight>
</lang>
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(defun horner (coeffs x)
(reduce #'(lambda (coef acc) (+ (* acc x) coef))
coeffs :from-end t :initial-value 0))</langsyntaxhighlight>
 
Alternate version using LOOP. Coefficients are passed in a vector.
 
<langsyntaxhighlight lang="lisp">(defun horner (x a)
(loop :with y = 0
:for i :from (1- (length a)) :downto 0
Line 370 ⟶ 672:
:finally (return y)))
 
(horner 1.414 #(-2 0 1))</langsyntaxhighlight>
 
=={{header|D}}==
The poly() function of the standard library std.math module uses Horner's rule:
<langsyntaxhighlight lang="d">void main() {
void main() {
import std.stdio, std.math;
Line 382 ⟶ 684:
poly(x,pp).writeln;
}
}</langsyntaxhighlight>
Basic implementation:
<langsyntaxhighlight lang="d">import std.stdio, std.traits;
 
CommonType!(U, V) horner(U, V)(U[] p, V x) pure nothrow @nogc {
Line 395 ⟶ 697:
void main() {
[-19, 7, -4, 6].horner(3.0).writeln;
}</langsyntaxhighlight>
More functional style:
<langsyntaxhighlight lang="d">import std.stdio, std.algorithm, std.range;
 
auto horner(T, U)(in T[] p, in U x) pure nothrow @nogc {
Line 405 ⟶ 707:
void main() {
[-19, 7, -4, 6].horner(3.0).writeln;
}</langsyntaxhighlight>
 
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|Classes,SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
function EvaluatePolynomial(Args: array of double; X: double): double;
{Evaluate polynomial using Horner's rule }
var I: integer;
begin
Result:=0;
for I:=High(Args) downto 0 do
Result:=(Result * X ) + Args[I];
end;
 
function GetPolynomialStr(Args: array of double; VarName: string): string;
{Return a string display the polynomial in normal format}
{for example: 6.0 X^3 - 4.0 X^2 + 7.0 X - 19.0}
var I: integer;
begin
Result:='';
for I:=High(Args) downto 0 do
begin
if Args[I]>0 then
begin
if I<>High(Args) then Result:=Result+' + ';
end
else Result:=Result+' - ';
Result:=Result+FloatToStrF(Abs(Args[I]),ffFixed,18,1);
if I>0 then Result:=Result+' '+VarName;
if I>1 then Result:=Result+'^'+IntToStr(I);
end;
end;
 
 
procedure ShowHornerPoly(Memo: TMemo; Args: array of double; X: double);
{Evaluate polynomial, show formated polynomal and the result}
var R: double;
begin
R:=EvaluatePolynomial(Args,X);
Memo.Lines.Add(FloatToStrF(R,ffFixed, 18,1));
Memo.Lines.Add(GetPolynomialStr(Args,'X'));
end;
 
 
procedure DoHornerPoly(Memo: TMemo);
begin
ShowHornerPoly(Memo,[-19, 7, -4, 6],3)
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
128.0
6.0 X^3 - 4.0 X^2 + 7.0 X - 19.0
</pre>
 
=={{header|E}}==
 
<langsyntaxhighlight lang="e">def makeHornerPolynomial(coefficients :List) {
def indexing := (0..!coefficients.size()).descending()
return def hornerPolynomial(x) {
Line 418 ⟶ 779:
return acc
}
}</langsyntaxhighlight>
 
<langsyntaxhighlight lang="e">? makeHornerPolynomial([-19, 7, -4, 6])(3)
# value: 128</langsyntaxhighlight>
 
=={{header|EasyLang}}==
{{trans|C}}
<syntaxhighlight>
func horner coeffs[] x .
for i = len coeffs[] downto 1
res = res * x + coeffs[i]
.
return res
.
print horner [ -19 7 -4 6 ] 3
</syntaxhighlight>
{{out}}
<pre>
128
</pre>
 
=={{header|EchoLisp}}==
=== Functional version ===
<langsyntaxhighlight lang="lisp">
(define (horner x poly)
(foldr (lambda (coeff acc) (+ coeff (* acc x))) 0 poly))
 
(horner 3 '(-19 7 -4 6)) → 128
</syntaxhighlight>
</lang>
=== Library ===
<langsyntaxhighlight lang="lisp">
(lib 'math)
Lib: math.lib loaded.
Line 439 ⟶ 816:
(poly->string 'x P) → 6x^3 -4x^2 +7x -19
(poly 3 P) → 128
</syntaxhighlight>
</lang>
 
=={{header|EDSAC order code}}==
<syntaxhighlight lang="edsac">
[Copyright <2021> <ERIK SARGSYAN>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.]
[Horner's rule for polynomial evaluation on EDSAC by Erik Sargsyan]
[EDSAC program, Initial Orders 2]
 
 
T 56 K
GK
[0] A 3 F
[1] T 9 F
[2] A 4 F [loading the address of the 1st array element into the accumulator]
[3] A 14 @ [add an instruction code with a zero address field]
[4] T 14 @ [writing the generated instruction, zeroing the accumulator]
[5 loop] A 1 F [load the counter of unprocessed array elements into the accumulator]
[6] S 21 @ [Subtract the constant = 1]
[7] E 10 @ [if (acc >= 0) goto 10 else end]
[8] T 300 F [zeroing the accumulator]
[9] E 0 F [breakpoint, end of program]
[10] T 1 F [update the counter value and reset the accumulator]
[11] V 2 F [multiplying the number in cell 2 by the number in the multiplier register]
[12] L 1024 F [shift the number in the accumulator 12 bits to the left]
[13] L 4 F [shift the number in the accumulator 4 bits to the left]
[14 r1] A 0 F [loading the value from cell 0 into the accumulator]
[15] T 2 F [writing this value to the working cell, zeroing the accumulator]
[16] A 21 @ [loading into accumulator constant value = 1]
[17] L 0 D [shift the number in the accumulator 1 bit to the left]
[18] A 14 @ [add the code of the instruction executed in the previous step]
[19] T 14 @ [write the generated instruction into memory]
[20] E 5 @ [repeat all operations; accumulator zeroed]
[21 const 1] P 0 D [1]
 
GK
[0] H 10 @ [writing X0 to the multiplier register]
[1] A 11 @ [loading into the accumulator of the degree of the polynomial]
[2] T 1 F [writing the degree of a polynomial in cell 1]
[3] A 13 @ [loading the leading coefficient into the accumulator]
[4] T 2 F [writing the senior coefficient to the working cell 2]
[5] A 12 @ [loading the address of the 1st array element into the accumulator]
[6] T 4 F [writing the address of the 1st element of the array]
[7] A 7 @ [\ call]
[8] G 56 F
[9] Z 0 F
[10] P 3 F [X0 is a fixed value of X, by which we calculate the value of the polynomial]
[11 power] P 2 F [polynomial degree times 2]
[12 addr] P 14 @ [address of the 1st element of the array]
[13] P 3 F [a4 = 6]
[14] P 8 D [a3 = 17]
[15] P 9 F [a2 = 18]
[16] P 12 D [a1 = 25]
[17] P 316 F [a0 = 632]
EZPF
</syntaxhighlight>
{{out}}
<pre>
Cell 2 will contain the number 12878
</pre>
 
=={{header|Elena}}==
{{trans|C#}}
ELENA 35.40 :
<langsyntaxhighlight lang="elena">import extensions.;
import system'routines.;
horner(coefficients,variable)
{
[
^ coefficients .clone; ().sequenceReverse; ().accumulate(Real new) withReal(:accumulator:coefficient),(accumulator,coefficient => accumulator * variable + coefficient))
}
]
public program()
{
[
console .printLine(horner((new real[]{-19.0r, 7.0r, -4.0r, 6.0r)}, 3.0r))
}</syntaxhighlight>
]</lang>
{{out}}
<pre>
Line 461 ⟶ 908:
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">horner = fn(list, x)-> List.foldr(list, 0, fn(c,acc)-> x*acc+c end) end
 
IO.puts horner.([-19,7,-4,6], 3)</langsyntaxhighlight>
 
{{out}}
Line 472 ⟶ 919:
=={{header|Emacs Lisp}}==
{{trans|Common Lisp}}
<syntaxhighlight lang="lisp">(require 'cl-lib)
<lang Emacs Lisp>
 
(defun horner (coeffs x)
(cl-reduce #'(lambda (coef acc) (+ (* acc x) coef) )
coeffs :from-end t :initial-value 0) )
 
(horner '(-19 7 -4 6) 3)</syntaxhighlight>
 
</lang>
{{out}}
<b>Output:</b>
 
<pre>
128
</pre>
 
=={{header|Erlang}}==
<langsyntaxhighlight lang="erlang">
horner(L,X) ->
lists:foldl(fun(C, Acc) -> X*Acc+C end,0, lists:reverse(L)).
t() ->
horner([-19,7,-4,6], 3).
</syntaxhighlight>
</lang>
 
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
<lang ERRE>
PROGRAM HORNER
 
Line 515 ⟶ 962:
PRINT(RES)
END PROGRAM
</syntaxhighlight>
</lang>
 
=={{header|Euler Math Toolbox}}==
 
<syntaxhighlight lang="euler math toolbox">
<lang Euler Math Toolbox>
>function horner (x,v) ...
$ n=cols(v); res=v{n};
Line 538 ⟶ 985:
3 2
6 x - 4 x + 7 x - 19
</syntaxhighlight>
</lang>
 
 
=={{header|F Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
let horner l x =
List.rev l |> List.fold ( fun acc c -> x*acc+c) 0
 
horner [-19;7;-4;6] 3
</syntaxhighlight>
</lang>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">: horner ( coeff x -- res )
[ <reversed> 0 ] dip '[ [ _ * ] dip + ] reduce ;</langsyntaxhighlight>
 
( scratchpad ) { -19 7 -4 6 } 3 horner .
Line 556 ⟶ 1,004:
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">: fhorner ( coeffs len F: x -- F: val )
0e
floats bounds ?do
Line 565 ⟶ 1,013:
create coeffs 6e f, -4e f, 7e f, -19e f,
 
coeffs 4 3e fhorner f. \ 128.</langsyntaxhighlight>
 
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<langsyntaxhighlight lang="fortran">program test_horner
 
implicit none
 
write (*, '(f5.1)') horner ((/[-19.0, 7.0, -4.0, 6.0/)], 3.0)
 
contains
Line 580 ⟶ 1,028:
 
implicit none
real, dimension (0:), intent (in) :: coeffs
real, intent (in) :: x
real :: res
integer :: i
 
res = 0.0coeffs(ubound(coeffs,1))
do i = size ubound(coeffs,1)-1, 10, -1
res = res * x + coeffs (i)
end do
Line 592 ⟶ 1,040:
end function horner
 
end program test_horner</langsyntaxhighlight>
Output:
<pre>128.0</pre>
 
=== Fortran 77 ===
<langsyntaxhighlight lang="fortran"> FUNCTION HORNER(N,A,X)
IMPLICIT NONE
INTEGER I,N
Line 606 ⟶ 1,054:
END DO
HORNER=Y
END</langsyntaxhighlight>
 
As a matter of fact, computing the derivative is not much more difficult (see [http://www.cs.berkeley.edu/~wkahan/Math128/Poly.pdf Roundoff in Polynomial Evaluation], W. Kahan, 1986). The following subroutine computes both polynomial value and derivative for argument x.
 
<langsyntaxhighlight lang="fortran"> SUBROUTINE HORNER2(N,A,X,Y,Z)
C COMPUTE POLYNOMIAL VALUE AND DERIVATIVE
C SEE "ROUNDOFF IN POLYNOMIAL EVALUATION", W. KAHAN, 1986
Line 623 ⟶ 1,071:
Z = Z*X + Y
10 Y = Y*X + A(I)
END</lang>
</syntaxhighlight>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">
Function AlgoritmoHorner(coeffs() As Integer, x As Integer) As Integer
Dim As Integer i, acumulador = 0
For i = Ubound(coeffs, 1) To 0 Step -1
acumulador = (acumulador * x) + coeffs(i)
Next i
Return acumulador
End Function
 
Dim As Integer x = 3
Dim As Integer coeficientes(3) = {-19, 7, -4, 6}
Print "Algoritmo de Horner para el polinomio 6*x^3 - 4*x^2 + 7*x - 19 para x = 3: ";
Print AlgoritmoHorner(coeficientes(), x)
End
</syntaxhighlight>
{{out}}
<pre>
Algoritmo de Horner para el polinomio 6*x^3 - 4*x^2 + 7*x - 19 para x = 3: 128
</pre>
 
=={{header|FunL}}==
{{trans|Haskell}}
<langsyntaxhighlight lang="funl">import lists.foldr
 
def horner( poly, x ) = foldr( \a, b -> a + b*x, 0, poly )
 
println( horner([-19, 7, -4, 6], 3) )</langsyntaxhighlight>
 
{{out}}
Line 637 ⟶ 1,107:
<pre>
128
</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">include "NSLog.incl"
 
local fn horner( coeffs as CFArrayRef, x as NSInteger ) as double
CFArrayRef reversedCoeffs
CFNumberRef num
double accumulator = 0.0
 
// Reverse coeffs array
reversedCoeffs = fn EnumeratorAllObjects( fn ArrayReverseObjectEnumerator( coeffs ) )
 
// Iterate over CFNumberRefs in reversed array, convert to double values, calculate and add to accumulator
for num in reversedCoeffs
accumulator = ( accumulator * x ) + fn NumberDoubleValue( num )
next
end fn = accumulator
 
CFArrayRef coeffs
 
coeffs = @[@-19.0, @7.0, @-4.0, @6.0]
NSLog( @"%7.1f", fn horner( coeffs, 3 ) )
 
coeffs = @[@4.0, @3.0, @2.0, @1.0]
NSLog( @"%7.1f", fn horner( coeffs, 10 ) )
 
coeffs = @[@1, @1, @0, @0, @1]
NSLog( @"%7.1f", fn horner( coeffs, 2 ) )
 
coeffs = @[@1.2, @2.3, @3.4, @4.5, @5.6]
NSLog( @"%7.1f", fn horner( coeffs, 8 ) )
 
coeffs = @[@1, @0, @1, @1, @1, @0, @0, @1]
NSLog( @"%7.1f", fn horner( coeffs, 2 ) )
 
HandleEvents</syntaxhighlight>
 
{{out}}
 
<pre>
128.0
1234.0
19.0
25478.8
157.0
</pre>
 
=={{header|GAP}}==
<langsyntaxhighlight lang="gap"># The idiomatic way to compute with polynomials
 
x := Indeterminate(Rationals, "x");
Line 671 ⟶ 1,187:
 
Horner(u, 3);
# 128</langsyntaxhighlight>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 687 ⟶ 1,203:
func main() {
fmt.Println(horner(3, []int64{-19, 7, -4, 6}))
}</langsyntaxhighlight>
Output:
<pre>
Line 695 ⟶ 1,211:
=={{header|Groovy}}==
Solution:
<langsyntaxhighlight lang="groovy">def hornersRule = { coeff, x -> coeff.reverse().inject(0) { accum, c -> (accum * x) + c } }</langsyntaxhighlight>
 
Test includes demonstration of [[currying]] to create polynomial functions of one variable from generic Horner's rule calculation. Also demonstrates constructing the derivative function for the given polynomial. And finally demonstrates in the Newton-Raphson method to find one of the polynomial's roots using the polynomial and derivative functions constructed earlier.
<langsyntaxhighlight lang="groovy">def coefficients = [-19g, 7g, -4g, 6g]
println (["p coefficients":coefficients])
 
Line 720 ⟶ 1,236:
 
def root = newtonRaphson(3g, testPoly, testDeriv)
println ([root:root.toString()[0..5], "p(root)":testPoly(root).toString()[0..5], "p'(root)":testDeriv(root).toString()[0..5]])</langsyntaxhighlight>
 
Output:
Line 732 ⟶ 1,248:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">horner :: (Num a) => a -> [a] -> a
horner x = foldr (\a b -> a + b*x) 0
 
main = print $ horner 3 [-19, 7, -4, 6]</langsyntaxhighlight>
 
=={{header|HicEst}}==
<langsyntaxhighlight HicEstlang="hicest">REAL :: x=3, coeffs(4)
DATA coeffs/-19.0, 7.0, -4.0, 6.0/
 
Line 749 ⟶ 1,265:
Horner = x*Horner + c(i)
ENDDO
END</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
 
<syntaxhighlight lang="icon">
<lang Icon>
procedure poly_eval (x, coeffs)
accumulator := 0
Line 764 ⟶ 1,280:
write (poly_eval (3, [-19, 7, -4, 6]))
end
</syntaxhighlight>
</lang>
 
=={{header|J}}==
'''Solution''':<langsyntaxhighlight lang="j">
horner =: (#."0 _ |.)~ NB. Tacit
horner =: 4 [: +`*/ '[: }: (+,@,. *&y)/x' NB. Alternate tacit (equivalent)
horner =: 4 : ' (+ *&y)/x' NB. Alternate explicit (equivalent)
 
</syntaxhighlight>
horner1 =: (#."0 _ |.)~
'''Example''':<syntaxhighlight lang="j"> _19 7 _4 6 horner 3
 
128</syntaxhighlight>
horner2=: [: +`*/ [: }: ,@,. NB. Alternate
</lang>
'''Example''':<lang j> _19 7 _4 6 horner 3
128</lang>
'''Note:'''<br>
The primitive verb <code>p.</code> would normally be used to evaluate polynomials.
<langsyntaxhighlight lang="j"> _19 7 _4 6 p. 3
128</langsyntaxhighlight>
 
=={{header|Java}}==
{{works with|Java|1.5+}}
<langsyntaxhighlight lang="java5">import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Line 806 ⟶ 1,319:
return accumulator;
}
}</langsyntaxhighlight>
Output:
<pre>128.0</pre>
Line 814 ⟶ 1,327:
 
{{trans|Haskell}}
<langsyntaxhighlight lang="javascript">function horner(coeffs, x) {
return coeffs.reduceRight( function(acc, coeff) { return(acc * x + coeff) }, 0);
}
console.log(horner([-19,7,-4,6],3)); // ==> 128
</syntaxhighlight>
</lang>
 
=={{header|jq}}==
<syntaxhighlight lang=jq>
# Input: an array of coefficients specifying the polynomial
# to be evaluated at $x, where .[0] is the constant
def horner($x):
. as $coefficients
| reduce range(length-1; -1; -1) as $i (0; . * $x + $coefficients[$i]);
 
# Example
[-19, 7, -4, 6] | horner(3)
</syntaxhighlight>
 
'''Invocation''': $JQ -n -f horner.jq
 
where $JQ is either jq or gojq
 
{{output}}
<pre>
128
</pre>
 
=={{header|Julia}}==
{{works with|Julia|01.6x}}
 
'''Imperative''':
<langsyntaxhighlight lang="julia">function horner(coefs, x)
s = coefs[end] * one(x)
for k in length(coefs)-1:-1:1
s = coefs[k] + x * s
Line 832 ⟶ 1,366:
end
 
@show horner([-19, 7, -4, 6], 3)</langsyntaxhighlight>
 
{{out}}
Line 838 ⟶ 1,372:
 
'''Functional''':
<langsyntaxhighlight lang="julia">horner2(coefs, x) = foldr((u, v) -> u + x * v, 0coefs, init=zero(promote_type(typeof(x),eltype(coefs))))
 
@show horner2([-19, 7, -4, 6], 3)</langsyntaxhighlight>
 
{{out}}
<pre>horner2([-19, 7, -4, 6], 3) = 128</pre>
 
'''Note''':
In Julia 1.4 or later one would normally use the built-in '''evalpoly''' function for this purpose:
<syntaxhighlight lang="julia">
@show evalpoly(3, [-19, 7, -4, 6]) </syntaxhighlight>
 
{{out}}
<pre>evalpoly(3, [-19, 7, -4, 6]) = 128</pre>
 
=={{header|K}}==
<syntaxhighlight lang="k">
<lang K>
horner:{y _sv|x}
horner[-19 7 -4 6;3]
128
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.2
 
fun horner(coeffs: DoubleArray, x: Double): Double {
Line 864 ⟶ 1,406:
val coeffs = doubleArrayOf(-19.0, 7.0, -4.0, 6.0)
println(horner(coeffs, 3.0))
}</langsyntaxhighlight>
 
{{out}}
Line 870 ⟶ 1,412:
128.0
</pre>
 
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
{def horner
{def horner.r
{lambda {:p :x :r}
{if {A.empty? :p}
then :r
else {horner.r {A.rest :p} :x {+ {A.first :p} {* :x :r}}}}}}
{lambda {:p :x}
{horner.r {A.reverse :p} :x 0}}}
 
{horner {A.new -19 7 -4 6} 3}
-> 128
 
{def φ {/ {+ 1 {sqrt 5}} 2}} = 1.618033988749895
{horner {A.new -1 -1 1} φ}
-> 2.220446049250313e-16 ~ 0
 
</syntaxhighlight>
 
=={{header|Liberty BASIC}}==
<langsyntaxhighlight lang="lb">src$ = "Hello"
coefficients$ = "-19 7 -4 6" ' list coefficients of all x^0..x^n in order
x = 3
Line 891 ⟶ 1,453:
horner = accumulator
end function
</langsyntaxhighlight>
 
=={{header|Logo}}==
<langsyntaxhighlight lang="logo">to horner :x :coeffs
if empty? :coeffs [output 0]
output (first :coeffs) + (:x * horner :x bf :coeffs)
end
 
show horner 3 [-19 7 -4 6] ; 128</langsyntaxhighlight>
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">function horners_rule( coeff, x )
local res = 0
for i = #coeff, 1, -1 do
Line 912 ⟶ 1,474:
x = 3
coefficients = { -19, 7, -4, 6 }
print( horners_rule( coefficients, x ) )</langsyntaxhighlight>
 
 
=={{header|Maple}}==
<syntaxhighlight lang="maple">
<lang Maple>
applyhorner:=(L::list,x)->foldl((s,t)->s*x+t,op(ListTools:-Reverse(L))):
 
Line 922 ⟶ 1,483:
 
applyhorner([-19,7,-4,6],3);
</syntaxhighlight>
</lang>
Output:
<pre>
Line 931 ⟶ 1,492:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">Horner[l_List, x_] := Fold[x #1 + #2 &, 0, l]
Horner[{6, -4, 7, -19}, x]
-> -19 + x (7 + x (-4 + 6 x))
 
-19 + x (7 + x (-4 + 6 x)) /. x -> 3
-> 128</langsyntaxhighlight>
 
=={{header|MATLAB}}==
<langsyntaxhighlight MATLABlang="matlab">function accumulator = hornersRule(x,coefficients)
 
accumulator = 0;
Line 947 ⟶ 1,508:
end
end</langsyntaxhighlight>
Output:
<langsyntaxhighlight MATLABlang="matlab">>> hornersRule(3,[-19, 7, -4, 6])
 
ans =
 
128</langsyntaxhighlight>
Matlab also has a built-in function "polyval" which uses Horner's Method to evaluate polynomials. The list of coefficients is in descending order of power, where as to task spec specifies ascending order.
<langsyntaxhighlight MATLABlang="matlab">>> polyval(fliplr([-19, 7, -4, 6]),3)
 
ans =
 
128</langsyntaxhighlight>
 
=={{header|Maxima}}==
<langsyntaxhighlight lang="maxima">/* Function horner already exists in Maxima, though it operates on expressions, not lists of coefficients */
horner(5*x^3+2*x+1);
x*(5*x^2+2)+1
Line 1,006 ⟶ 1,567:
 
poleval([0, 0, 0, 0, 1], x);
x^4</langsyntaxhighlight>
 
=={{header|Mercury}}==
<langsyntaxhighlight lang="mercury">
:- module horner.
:- interface.
Line 1,023 ⟶ 1,584:
 
horner(X, Cs) = list.foldr((func(C, Acc) = Acc * X + C), Cs, 0).
</syntaxhighlight>
</lang>
 
=={{header|МК-61/52}}==
<syntaxhighlight lang="text">ИП0 1 + П0
ИПE ИПD * КИП0 + ПE
ИП0 1 - x=0 04
ИПE С/П</langsyntaxhighlight>
 
''Input:'' Р1:РС - coefficients, Р0 - number of the coefficients, РD - ''x''.
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE Horner;
FROM RealStr IMPORT RealToStr;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
Line 1,062 ⟶ 1,623:
WriteLn;
ReadChar
END Horner.</langsyntaxhighlight>
 
=={{header|NetRexx}}==
<langsyntaxhighlight lang="netrexx">/* NetRexx */
options replace format comments java crossref savelog symbols nobinary
 
Line 1,076 ⟶ 1,637:
End
Say r
Say 6*x**3-4*x**2+7*x-19</langsyntaxhighlight>
'''Output:'''
<pre>128
Line 1,082 ⟶ 1,643:
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim"># You can also just use `reversed` proc from stdlib `algorithm` module
iterator reversed[T](x: openArray[T]): T =
for i in countdown(x.high, x.low):
Line 1,091 ⟶ 1,652:
result = result * x + c
echo horner([-19, 7, -4, 6], 3)</langsyntaxhighlight>
 
=={{header|Oberon-2}}==
{{works with|oo2c}}
<langsyntaxhighlight lang="oberon2">
MODULE HornerRule;
IMPORT
Line 1,124 ⟶ 1,685:
Out.Int(Eval(coefs^,4,3),0);Out.Ln
END HornerRule.
</syntaxhighlight>
</lang>
{{out}}
<pre>
128
</pre>
 
=={{header|Objeck}}==
<syntaxhighlight lang="objeck">
class Horner {
function : Main(args : String[]) ~ Nil {
coeffs := Collection.FloatVector->New();
coeffs->AddBack(-19.0);
coeffs->AddBack(7.0);
coeffs->AddBack(-4.0);
coeffs->AddBack(6.0);
PolyEval(coeffs, 3)->PrintLine();
}
function : PolyEval(coefficients : Collection.FloatVector , x : Float) ~ Float {
accumulator := coefficients->Get(coefficients->Size() - 1);
for(i := coefficients->Size() - 2; i > -1; i -= 1;) {
accumulator := (accumulator * x) + coefficients->Get(i);
};
return accumulator;
}
}
</syntaxhighlight>
 
=={{header|Objective-C}}==
{{works with|Mac OS X|10.6+}} Using blocks
<langsyntaxhighlight lang="objc">#import <Foundation/Foundation.h>
 
typedef double (^mfunc)(double, double);
Line 1,173 ⟶ 1,757:
}
return 0;
}</langsyntaxhighlight>
 
=={{header|Objeck}}==
<lang objeck>
class Horner {
function : Main(args : String[]) ~ Nil {
coeffs := Collection.FloatVector->New();
coeffs->AddBack(-19.0);
coeffs->AddBack(7.0);
coeffs->AddBack(-4.0);
coeffs->AddBack(6.0);
PolyEval(coeffs, 3)->PrintLine();
}
function : PolyEval(coefficients : Collection.FloatVector , x : Float) ~ Float {
accumulator := coefficients->Get(coefficients->Size() - 1);
for(i := coefficients->Size() - 2; i > -1; i -= 1;) {
accumulator := (accumulator * x) + coefficients->Get(i);
};
return accumulator;
}
}
</lang>
 
=={{header|OCaml}}==
 
<langsyntaxhighlight lang="ocaml"># let horner coeffs x =
List.fold_left (fun acc coef -> acc * x + coef) 0 (List.rev coeffs) ;;
val horner : int list -> int -> int = <fun>
Line 1,206 ⟶ 1,767:
# let coeffs = [-19; 7; -4; 6] in
horner coeffs 3 ;;
- : int = 128</langsyntaxhighlight>
It's also possible to do fold_right instead of reversing and doing fold_left; but fold_right is not tail-recursive.
 
=={{header|Octave}}==
<langsyntaxhighlight lang="octave">function r = horner(a, x)
r = 0.0;
for i = length(a):-1:1
Line 1,217 ⟶ 1,778:
endfunction
 
horner([-19, 7, -4, 6], 3)</langsyntaxhighlight>
 
=={{header|ooRexx}}==
<langsyntaxhighlight lang="oorexx">/* Rexx ---------------------------------------------------------------
* 04.03.2014 Walter Pachl
*--------------------------------------------------------------------*/
Line 1,231 ⟶ 1,792:
End
Say r
Say 6*x**3-4*x**2+7*x-19</langsyntaxhighlight>
'''Output:'''
<pre>128
128</pre>
 
=={{header|Oz}}==
<langsyntaxhighlight lang="oz">declare
fun {Horner Coeffs X}
{FoldL1 {Reverse Coeffs}
Line 1,249 ⟶ 1,810:
end
in
{Show {Horner [~19 7 ~4 6] 3}}</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
Also note that Pari has a polynomial type. Evaluating these is as simple as <code>subst(P,variable(P),x)</code>.
<langsyntaxhighlight lang="parigp">horner(v,x)={
my(s=0);
forstep(i=#v,1,-1,s=s*x+v[i]);
s
};</langsyntaxhighlight>
 
=={{header|Pascal}}==
<langsyntaxhighlight lang="pascal">Program HornerDemo(output);
 
function horner(a: array of double; x: double): double;
Line 1,277 ⟶ 1,838:
write ('Horner calculated polynomial of 6*x^3 - 4*x^2 + 7*x - 19 for x = 3: ');
writeln (horner (poly, 3.0):8:4);
end.</langsyntaxhighlight>
Output:
<pre>Horner calculated polynomial of 6*x^3 - 4*x^2 + 7*x - 19 for x = 3: 128.0000
Line 1,283 ⟶ 1,844:
 
=={{header|Perl}}==
<langsyntaxhighlight Perllang="perl">use 5.10.0;
use strict;
use warnings;
Line 1,296 ⟶ 1,857:
my @coeff = (-19.0, 7, -4, 6);
my $x = 3;
say horner @coeff, $x;</langsyntaxhighlight>
 
===<!-- Perl -->Functional version===
<langsyntaxhighlight lang="perl">use strict;
use List::Util qw(reduce);
 
Line 1,309 ⟶ 1,870:
my @coeff = (-19.0, 7, -4, 6);
my $x = 3;
print horner(\@coeff, $x), "\n";</langsyntaxhighlight>
 
===<!-- Perl -->Recursive version===
<langsyntaxhighlight lang="perl">sub horner {
my ($coeff, $x) = @_;
@$coeff and
Line 1,318 ⟶ 1,879:
}
print horner( [ -19, 7, -4, 6 ], 3 );</langsyntaxhighlight>
 
=={{header|Perl 6}}==
<lang perl6>sub horner ( @coeffs, $x ) {
@coeffs.reverse.reduce: { $^a * $x + $^b };
}
 
say horner( [ -19, 7, -4, 6 ], 3 );</lang>
 
A recursive version would spare us the need for reversing the list of coefficients. However, special care must be taken in order to write it, because the way Perl 6 implements lists is not optimized for this kind of treatment. [[Lisp]]-style lists are, and fortunately it is possible to emulate them with [http://doc.perl6.org/type/Pair Pairs] and the reduction meta-operator:
 
<lang perl6>multi horner(Numeric $c, $) { $c }
multi horner(Pair $c, $x) {
$c.key + $x * horner( $c.value, $x )
}
say horner( [=>](-19, 7, -4, 6 ), 3 );</lang>
 
We can also use the composition operator:
<lang perl6>sub horner ( @coeffs, $x ) {
([o] map { $_ + $x * * }, @coeffs)(0);
}
say horner( [ -19, 7, -4, 6 ], 3 );</lang>
 
{{out}}
<pre>128</pre>
 
One advantage of using the composition operator is that it allows for the use of an infinite list of coefficients.
<lang perl6>sub horner ( @coeffs, $x ) {
map { .(0) }, [\o] map { $_ + $x * * }, @coeffs;
}
say horner( [ 1 X/ (1, |[\*] 1 .. *) ], i*pi )[20];
</lang>
{{out}}
<pre>-0.999999999924349-5.28918515954219e-10i</pre>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>function horner(atom x, sequence coeff)
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
atom res = 0
<span style="color: #008080;">function</span> <span style="color: #000000;">horner</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">coeff</span><span style="color: #0000FF;">)</span>
for i=length(coeff) to 1 by -1 do
<span style="color: #004080;">atom</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
res = res*x + coeff[i]
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">coeff</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
end for
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">*</span><span style="color: #000000;">x</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">coeff</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
return res
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
?horner(3,{-19, 7, -4, 6})</lang>
<span style="color: #0000FF;">?</span><span style="color: #000000;">horner</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,{-</span><span style="color: #000000;">19</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">6</span><span style="color: #0000FF;">})</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,372 ⟶ 1,900:
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php"><?php
function horner($coeff, $x) {
$result = 0;
Line 1,383 ⟶ 1,911:
$x = 3;
echo horner($coeff, $x), "\n";
?></langsyntaxhighlight>
 
===Functional version===
{{works with|PHP|5.3+}}
<langsyntaxhighlight lang="php"><?php
function horner($coeff, $x) {
return array_reduce(array_reverse($coeff), function ($a, $b) use ($x) { return $a * $x + $b; }, 0);
Line 1,395 ⟶ 1,923:
$x = 3;
echo horner($coeff, $x), "\n";
?></langsyntaxhighlight>
 
=={{header|Picat}}==
===Recursion===
<syntaxhighlight lang="picat">horner([],_X,0).
horner([H|T],X,V) :-
horner(T,X,V1),
V = V1 * X + H.</syntaxhighlight>
 
===Iterative===
<syntaxhighlight lang="picat">horner2(Coeff, X, V) =>
Acc = 0,
foreach(I in Coeff.length..-1..1)
Acc := Acc*X + Coeff[I]
end,
V = Acc.</syntaxhighlight>
 
===Functional approach===
<syntaxhighlight lang="picat">h3(X,A,B) = A+B*X.
horner3(Coeff, X) = fold($h3(X),0,Coeff.reverse()).</syntaxhighlight>
 
===Test===
<syntaxhighlight lang="picat">go =>
horner([-19, 7, -4, 6], 3, V),
println(V),
horner2([-19, 7, -4, 6], 3, V2),
println(V2),
V3 = horner3([-19, 7, -4, 6], 3),
println(V3),
nl.</syntaxhighlight>
 
{{out}}
<pre>128
128
128</pre>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de horner (Coeffs X)
(let Res 0
(for C (reverse Coeffs)
(setq Res (+ C (* X Res))) ) ) )</langsyntaxhighlight>
<langsyntaxhighlight PicoLisplang="picolisp">: (horner (-19.0 7.0 -4.0 6.0) 3.0)
-> 128</langsyntaxhighlight>
 
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
<lang PL/I>
declare (i, n) fixed binary, (x, value) float; /* 11 May 2010 */
get (x);
Line 1,419 ⟶ 1,983:
put (value);
end;
</syntaxhighlight>
</lang>
 
=={{header|Potion}}==
<langsyntaxhighlight lang="potion">horner = (x, coef) :
result = 0
coef reverse each (a) :
Line 1,430 ⟶ 1,994:
.
 
horner(3, (-19, 7, -4, 6)) print</langsyntaxhighlight>
 
=={{header|PowerShell}}==
{{works with|PowerShell|4.0}}
<syntaxhighlight lang="powershell">
<lang PowerShell>
function horner($coefficients, $x) {
$accumulator = 0
Line 1,445 ⟶ 2,009:
$x = 3
horner $coefficients $x
</syntaxhighlight>
</lang>
<b>Output:</b>
<pre>
Line 1,453 ⟶ 2,017:
=={{header|Prolog}}==
Tested with SWI-Prolog. Works with other dialects.
<langsyntaxhighlight Prologlang="prolog">horner([], _X, 0).
 
horner([H|T], X, V) :-
horner(T, X, V1),
V is V1 * X + H.
</syntaxhighlight>
</lang>
Output :
<langsyntaxhighlight Prologlang="prolog"> ?- horner([-19, 7, -4, 6], 3, V).
V = 128.</langsyntaxhighlight>
 
===Functional approach===
Works with SWI-Prolog and module lambda, written by <b>Ulrich Neumerkel</b> found there http://www.complang.tuwien.ac.at/ulrich/Prolog-inedit/lambda.pl
<langsyntaxhighlight Prologlang="prolog">:- use_module(library(lambda)).
 
 
Line 1,477 ⟶ 2,041:
f_horner(L, V, R) :-
foldr(\X^Y^Z^(Z is X * V + Y), 0, L, R).
</syntaxhighlight>
</lang>
 
===Functional syntax (Ciao)===
Works with Ciao (https://github.com/ciao-lang/ciao) and the fsyntax package:
<syntaxhighlight lang="prolog">
<lang Prolog>
:- module(_, [horner/3], [fsyntax, hiord]).
:- use_module(library(hiordlib)).
horner(L, X) := ~foldr((''(H,V0,V) :- V is V0*X + H), L, 0).
</syntaxhighlight>
</lang>
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">Procedure Horner(List Coefficients(), b)
Define result
ForEach Coefficients()
Line 1,494 ⟶ 2,058:
Next
ProcedureReturn result
EndProcedure</langsyntaxhighlight>
 
'''Implemented as
<langsyntaxhighlight PureBasiclang="purebasic">NewList a()
AddElement(a()): a()= 6
AddElement(a()): a()= -4
AddElement(a()): a()= 7
AddElement(a()): a()=-19
Debug Horner(a(),3)</langsyntaxhighlight>
'''Outputs
128
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">>>> def horner(coeffs, x):
acc = 0
for c in reversed(coeffs):
Line 1,514 ⟶ 2,078:
 
>>> horner( (-19, 7, -4, 6), 3)
128</langsyntaxhighlight>
 
===Functional version===
<langsyntaxhighlight lang="python">>>> try: from functools import reduce
except: pass
 
Line 1,524 ⟶ 2,088:
 
>>> horner( (-19, 7, -4, 6), 3)
128</langsyntaxhighlight>
 
==={{libheader|NumPy}}===
<langsyntaxhighlight lang="python">>>> import numpy
>>> numpy.polynomial.polynomial.polyval(3, (-19, 7, -4, 6))
128.0</langsyntaxhighlight>
 
=={{header|R}}==
Procedural style:
<langsyntaxhighlight lang="r">horner <- function(a, x) {
y <- 0
for(c in rev(a)) {
Line 1,541 ⟶ 2,105:
}
 
cat(horner(c(-19, 7, -4, 6), 3), "\n")</langsyntaxhighlight>
Functional style:
<langsyntaxhighlight lang="r">horner <- function(x, v) {
Reduce(v, right=T, f=function(a, b) {
b * x + a
})
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,558 ⟶ 2,122:
Translated from Haskell
 
<langsyntaxhighlight lang="racket">
#lang racket
(define (horner x l)
Line 1,565 ⟶ 2,129:
(horner 3 '(-19 7 -4 6))
 
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" line>sub horner ( @coeffs, $x ) {
@coeffs.reverse.reduce: { $^a * $x + $^b };
}
 
say horner( [ -19, 7, -4, 6 ], 3 );</syntaxhighlight>
 
A recursive version would spare us the need for reversing the list of coefficients. However, special care must be taken in order to write it, because the way Raku implements lists is not optimized for this kind of treatment. [[Lisp]]-style lists are, and fortunately it is possible to emulate them with [http://doc.raku.org/type/Pair Pairs] and the reduction meta-operator:
 
<syntaxhighlight lang="raku" line>multi horner(Numeric $c, $) { $c }
multi horner(Pair $c, $x) {
$c.key + $x * horner( $c.value, $x )
}
say horner( [=>](-19, 7, -4, 6 ), 3 );</syntaxhighlight>
 
We can also use the composition operator:
<syntaxhighlight lang="raku" line>sub horner ( @coeffs, $x ) {
([o] map { $_ + $x * * }, @coeffs)(0);
}
say horner( [ -19, 7, -4, 6 ], 3 );</syntaxhighlight>
 
{{out}}
<pre>128</pre>
 
One advantage of using the composition operator is that it allows for the use of an infinite list of coefficients.
<syntaxhighlight lang="raku" line>sub horner ( @coeffs, $x ) {
map { .(0) }, [\o] map { $_ + $x * * }, @coeffs;
}
say horner( [ 1 X/ (1, |[\*] 1 .. *) ], i*pi )[20];
</syntaxhighlight>
{{out}}
<pre>-0.999999999924349-5.28918515954219e-10i</pre>
 
=={{header|Rascal}}==
<langsyntaxhighlight lang="rascal">import List;
 
public int horners_rule(list[int] coefficients, int x){
Line 1,575 ⟶ 2,176:
acc = acc * x + i;}
return acc;
}</langsyntaxhighlight>
A neater and shorter solution using a reducer:
<langsyntaxhighlight lang="rascal">public int horners_rule2(list[int] coefficients, int x) = (0 | it * x + c | c <- reverse(coefficients));</langsyntaxhighlight>
Output:
<langsyntaxhighlight lang="rascal">rascal>horners_rule([-19, 7, -4, 6], 3)
int: 128
 
rascal>horners_rule2([-19, 7, -4, 6], 3)
int: 128</langsyntaxhighlight>
 
=={{header|REBOL}}==
 
<langsyntaxhighlight lang="rebol">REBOL []
 
horner: func [coeffs x] [
Line 1,597 ⟶ 2,198:
]
 
print horner [-19 7 -4 6] 3</langsyntaxhighlight>
 
=={{header|REXX}}==
===version 1===
<langsyntaxhighlight lang="rexx">/*REXX program demonstrates using Horner's rule for polynomial evaluation. */
numeric digits 30 /*use extra numeric precision. */
parse arg x poly /*get value of X and the coefficients. */
Line 1,619 ⟶ 2,220:
end /*j*/
say /*display a blank line for readability.*/
say ' answer = ' a /*stick a fork in it, we're all done. */</langsyntaxhighlight>
'''output''' &nbsp; when the following is used for input: &nbsp; <tt> 3 &nbsp; -19 &nbsp; 7 &nbsp; -4 &nbsp; 6 </tt>
<pre>
Line 1,630 ⟶ 2,231:
 
===version 2===
<langsyntaxhighlight lang="rexx">/* REXX ---------------------------------------------------------------
* 27.07.2012 Walter Pachl
* coefficients reversed to descending order of power
Line 1,674 ⟶ 2,275:
Say ' equation = ' equ
Say ' '
Say ' result = ' a</langsyntaxhighlight>
{{out}}
<pre> x = 3
Line 1,683 ⟶ 2,284:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
coefficients = [-19, 7, -4, 6]
see "x = 3" + nl +
Line 1,696 ⟶ 2,297:
next
return w
</syntaxhighlight>
</lang>
Output:
<pre>
Line 1,712 ⟶ 2,313:
 
This said, solution to the problem is
<syntaxhighlight lang="rlab">
<lang RLaB>
>> a = [6, -4, 7, -19]
6 -4 7 -19
Line 1,720 ⟶ 2,321:
128
 
</syntaxhighlight>
</lang>
 
=={{header|RPL}}==
===Translation of the algorithm===
Following the pseudocode given [https://web.physics.utah.edu/~detar/lessons/c++/array/node2.html here] to the letter:
≪ OVER DUP SIZE GET → a x0 p
≪ a SIZE 1 - 1 '''FOR''' j
'a(j)+x0*p' EVAL 'p' STO -1 '''STEP'''
p
≫ ≫ ‘'''HORNR'''’ STO
 
===Idiomatic one-liner===
Reducing the loop to its simplest form: one memory call, one multiplication and one addition.
≪ → x0 ≪ LIST→ 2 SWAP '''START''' x0 * + '''NEXT''' ≫ ≫ ‘'''HORNR'''’ STO
{{in}}
<pre>
{ -19 7 -4 6 } 3 HORNR
</pre>
{{out}}
<pre>
1: 128
</pre>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">def horner(coeffs, x)
coeffs.reverse.inject(0) {|acc, coeff| acc * x + coeff}
end
p horner([-19, 7, -4, 6], 3) # ==> 128</langsyntaxhighlight>
 
=={{header|Rust}}==
<lang rust>fn horner(v: &[f64], x: f64) -> f64 {
v.iter().rev().fold(0.0, |acc, coeff| acc*x + coeff)
}
 
fn main() {
let v = [-19., 7., -4., 6.];
println!("result: {}", horner(&v, 3.0));
}</lang>
 
A generic version that works with any number type and much more. So much more, it's hard to imagine what that may be useful for. <br>
'''Uses a gated feature (the Zero trait), only works in unstable Rust'''
<lang rust>#![feature(zero_one)
use std::num::Zero;
use std::ops::{Mul, Add};
 
fn horner<Arr,Arg, Out>(v: &[Arr], x: Arg) -> Out
where Arr: Clone,
Arg: Clone,
Out: Zero + Mul<Arg, Output=Out> + Add<Arr, Output=Out>,
{
v.iter().rev().fold(Zero::zero(), |acc, coeff| acc*x.clone() + coeff.clone())
}
 
fn main() {
let v = [-19., 7., -4., 6.];
let output: f64 = horner(&v, 3.0);
println!("result: {}", output);
}</lang>
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">coef$ = "-19 7 -4 6" ' list coefficients of all x^0..x^n in order
x = 3
print horner(coef$,x) '128
Line 1,775 ⟶ 2,367:
next
horner = accum
end function</langsyntaxhighlight>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">fn horner(v: &[f64], x: f64) -> f64 {
v.iter().rev().fold(0.0, |acc, coeff| acc*x + coeff)
}
 
fn main() {
let v = [-19., 7., -4., 6.];
println!("result: {}", horner(&v, 3.0));
}</syntaxhighlight>
 
A generic version that works with any number type and much more. So much more, it's hard to imagine what that may be useful for.
<syntaxhighlight lang="rust">extern crate num; // 0.2.0
use num::Zero;
use std::ops::{Add, Mul};
 
fn horner<Arr, Arg, Out>(v: &[Arr], x: Arg) -> Out
where
Arr: Clone,
Arg: Clone,
Out: Zero + Mul<Arg, Output = Out> + Add<Arr, Output = Out>,
{
v.iter()
.rev()
.fold(Zero::zero(), |acc, coeff| acc * x.clone() + coeff.clone())
}
 
fn main() {
let v = [-19., 7., -4., 6.];
let output: f64 = horner(&v, 3.0);
println!("result: {}", output);
}</syntaxhighlight>
 
=={{header|Sather}}==
<langsyntaxhighlight lang="sather">class MAIN is
action(s, e, x:FLT):FLT is
Line 1,792 ⟶ 2,416:
#OUT + horner(|-19.0, 7.0, -4.0, 6.0|, 3.0) + "\n";
end;
end;</langsyntaxhighlight>
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">def horner(coeffs:List[Double], x:Double)=
coeffs.reverse.foldLeft(0.0){(a,c)=> a*x+c}
</syntaxhighlight>
</lang>
<langsyntaxhighlight lang="scala">val coeffs=List(-19.0, 7.0, -4.0, 6.0)
println(horner(coeffs, 3))
-> 128.0
</syntaxhighlight>
</lang>
 
=={{header|Scheme}}==
{{Works with|Scheme|R<math>^5</math>RS}}
<langsyntaxhighlight lang="scheme">(define (horner lst x)
(define (*horner lst x acc)
(if (null? lst)
Line 1,813 ⟶ 2,437:
 
(display (horner (list -19 7 -4 6) 3))
(newline)</langsyntaxhighlight>
Output:
<syntaxhighlight lang="text">128</langsyntaxhighlight>
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "float.s7i";
 
Line 1,839 ⟶ 2,463:
begin
writeln(horner(coeffs, 3.0) digits 1);
end func;</langsyntaxhighlight>
 
Output:
Line 1,848 ⟶ 2,472:
=={{header|Sidef}}==
Functional:
<langsyntaxhighlight lang="ruby">func horner(coeff, x) {
coeff.reverse.reduce { |a,b| a*x + b };
}
 
say horner([-19, 7, -4, 6], 3); # => 128</langsyntaxhighlight>
 
Recursive:
<langsyntaxhighlight lang="ruby">func horner(coeff, x) {
(coeff.len > 0) \
&&? (coeff[0] + x*horner(coeff.ftlast(-1), x));
: 0
}
 
say horner([-19, 7, -4, 6], 3); # => 128</langsyntaxhighlight>
 
=={{header|Smalltalk}}==
{{works with|GNU Smalltalk}}
<langsyntaxhighlight lang="smalltalk">OrderedCollection extend [
horner: aValue [
^ self reverse inject: 0 into: [:acc :c | acc * aValue + c].
Line 1,870 ⟶ 2,495:
].
 
(#(-19 7 -4 6) asOrderedCollection horner: 3) displayNl.</langsyntaxhighlight>
 
=={{header|Standard ML}}==
<langsyntaxhighlight lang="sml">(* Assuming real type for coefficients and x *)
fun horner coeffList x = foldr (fn (a, b) => a + b * x) (0.0) coeffList</langsyntaxhighlight>
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">func horner(coefs: [Double], x: Double) -> Double {
return reduce(lazy(coefs).reverse(), 0) { $0 * x + $1 }
}
 
println(horner([-19, 7, -4, 6], 3))</langsyntaxhighlight>
{{out}}
<pre>128.0</pre>
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
proc horner {coeffs x} {
set y 0
Line 1,893 ⟶ 2,518:
}
return $y
}</langsyntaxhighlight>
Demonstrating:
<langsyntaxhighlight lang="tcl">puts [horner {-19 7 -4 6} 3]</langsyntaxhighlight>
Output:
<pre>128</pre>
Line 1,903 ⟶ 2,528:
Note: this function, "Horner", gets its coefficients as a ParamArray which has no specified length. This array collect all arguments after the first one(s). This means you must specify x first, then the coefficients.
 
<syntaxhighlight lang="vba">
<lang VBA>
Public Function Horner(x, ParamArray coeff())
Dim result As Double
Line 1,916 ⟶ 2,541:
Horner = result
End Function
</syntaxhighlight>
</lang>
 
Output:
Line 1,923 ⟶ 2,548:
128
</pre>
 
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
Function horners_rule(coefficients,x)
accumulator = 0
For i = UBound(coefficients) To 0 Step -1
accumulator = (accumulator * x) + coefficients(i)
Next
horners_rule = accumulator
End Function
 
WScript.StdOut.WriteLine horners_rule(Array(-19,7,-4,6),3)
</syntaxhighlight>
 
{{Out}}
<pre>128</pre>
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<syntaxhighlight lang="vbnet">Module Module1
 
Function Horner(coefficients As Double(), variable As Double) As Double
Return coefficients.Reverse().Aggregate(Function(acc, coeff) acc * variable + coeff)
End Function
 
Sub Main()
Console.WriteLine(Horner({-19.0, 7.0, -4.0, 6.0}, 3.0))
End Sub
 
End Module</syntaxhighlight>
{{out}}
<pre>128</pre>
 
=={{header|Visual FoxPro}}==
===Coefficients in ascending order.===
<langsyntaxhighlight lang="vfp">
LOCAL x As Double
LOCAL ARRAY aCoeffs[1]
Line 1,946 ⟶ 2,603:
RETURN s
ENDFUNC
</syntaxhighlight>
</lang>
 
===Coefficients in descending order.===
<langsyntaxhighlight lang="vfp">
LOCAL x As Double
LOCAL ARRAY aCoeffs[1]
Line 1,968 ⟶ 2,625:
RETURN s
ENDFUNC
</syntaxhighlight>
</lang>
 
=={{header|VBScriptV (Vlang)}}==
<syntaxhighlight lang="v (vlang)">fn horner(x i64, c []i64) i64 {
<lang vb>
mut acc := i64(0)
Function horners_rule(coefficients,x)
for i := c.len - 1; i >= 0; i-- {
accumulator = 0
acc = acc*x + c[i]
For i = UBound(coefficients) To 0 Step -1
}
accumulator = (accumulator * x) + coefficients(i)
return acc
Next
}
horners_rule = accumulator
End Function
fn main() {
println(horner(3, [i64(-19), 7, -4, 6]))
}</syntaxhighlight>
 
{{out}}
WScript.StdOut.WriteLine horners_rule(Array(-19,7,-4,6),3)
</langpre>
128
</pre>
 
=={{Outheader|Wren}}==
<syntaxhighlight lang="wren">var horner = Fn.new { |x, c|
<pre>128</pre>
var count = c.count
if (count == 0) return 0
return (count-1..0).reduce(0) { |acc, index| acc*x + c[index] }
}
 
System.print(horner.call(3, [-19, 7, -4, 6]))</syntaxhighlight>
 
{{out}}
<pre>
128
</pre>
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">code IntOut=11;
 
func Horner(X, N, C); \Return value of polynomial in X
Line 1,998 ⟶ 2,671:
];
 
IntOut(0, Horner(3, 4, [-19, 7, -4, 6]));</langsyntaxhighlight>
 
Output:
Line 2,006 ⟶ 2,679:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">fcn horner(coeffs,x)
{ coeffs.reverse().reduce('wrap(a,coeff){ a*x + coeff },0.0) }</langsyntaxhighlight>
{{out}}
<pre>
1,973

edits