user
A fast scheme for evaluating a polynomial such as:

:   −
    19
    +
    7
    x
    −
    4

    x

    2
    +
    6

    x

    3

    {\displaystyle {\displaystyle -19+7x-4x^{2}+6x^{3}\,}}

when

:   x
    =
    3

    {\displaystyle {\displaystyle x=3\;}}
    .

is to arrange the computation as follows:

:   (
    (
    (
    (
    0
    )
    x
    +
    6
    )
    x
    +
    (
    −
    4
    )
    )
    x
    +
    7
    )
    x
    +
    (
    −
    19
    )

    {\displaystyle {\displaystyle ((((0)x+6)x+(-4))x+7)x+(-19)\;}}

And compute the result from the innermost brackets outwards as in this pseudocode:

```
coefficients := [-19, 7, -4, 6] # list coefficients of all x^0..x^n in order
x := 3
accumulator := 0
for i in length(coefficients) downto 1 do
    # Assumes 1-based indexing for arrays
    accumulator := ( accumulator * x ) + coefficients[i]
done
# accumulator now has the answer

```

**Task Description**

:   Create a routine that takes a list of coefficients of a polynomial in order of increasing powers of x; together with a value of x to compute its value at, and return the value of the polynomial at that value using Horner's rule.

Cf. Formal power series
assistant
(formerly Perl 6)

```
sub horner ( @coeffs, $x ) {
    @coeffs.reverse.reduce: { $^a * $x + $^b };
}

say horner( [ -19, 7, -4, 6 ], 3 );

```

Here is a recursive version :

```
multi horner(@c, $x) {
    @c > 1 ?? @c.head + $x * samewith(@c.tail(*-1), $x) !! @c.pick
}
 
say horner( [-19, 7, -4, 6 ], 3 );

```

We can also use the composition operator:

```
sub horner ( @coeffs, $x ) {
    ([o] map { $_ + $x * * }, @coeffs)(0);
}
 
say horner( [ -19, 7, -4, 6 ], 3 );

```

Output:

```
128
```

One advantage of using the composition operator is that it allows for the use of an infinite list of coefficients.

```
sub horner ( @coeffs, $x ) {
    map { .(0) }, [\o] map { $_ + $x * * }, @coeffs;
}
 
say horner( [ 1 X/ (1, |[\*] 1 .. *) ], i*pi )[20];

```

Output:

```
-0.999999999924349-5.28918515954219e-10i
```