# Horner's rule for polynomial evaluation

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

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

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](https://rosettacode.org/wiki/Lisp)-style lists are, and fortunately it is possible to emulate them with [Pairs](http://doc.raku.org/type/Pair) and the reduction meta-operator:

```perl
multi horner(Numeric $c, $) { $c }
multi horner(Pair $c, $x) {
    $c.key + $x * horner( $c.value, $x ) 
}
 
say horner( [=>](-19, 7, -4, 6 ), 3 );
```

We can also use the composition operator:

```perl
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.

```perl
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
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://trizen.gitbook.io/perl6-rosettacode/programming_tasks/h/horners_rule_for_polynomial_evaluation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
