# Some queries about macro Horner

**URL:** <https://discourse.julialang.org/t/some-queries-about-macro-horner/8258>\
**Category:** General Usage\
**Created:** [January 10, 2018, 12:22am UTC](https://discourse.julialang.org/t/some-queries-about-macro-horner/8258 "2018-01-10T00:22:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sabodhapati](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sabodhapati/32/2782_2.png) [@Sabodhapati](https://discourse.julialang.org/u/Sabodhapati)\
**Post date:** [January 10, 2018, 12:22am UTC](https://discourse.julialang.org/t/some-queries-about-macro-horner/8258/1 "2018-01-10T00:22:35Z")

</div>

In Macros for numerical performance: Horner’s method[https://github.com/stevengj/18S096-iap17/blob/master/lecture4/Metaprogramming.ipynb](https://github.com/stevengj/18S096-iap17/blob/master/lecture4/Metaprogramming.ipynb), macro hornor is defined as follows:

# copied from base/math.jl

```julia
macro horner(x, p...)
    ex = esc(p[end])
    for i = length(p)-1:-1:1
        ex = :( $(esc(p[i])) + t * $ex )
    end
    Expr(:block, :(t = $(esc(x))), ex)
end

```

so why use a new variable `t` in for loop, and after the loop still use `Expr(...)`, not just defined the hornor as follows:

```julia
macro horner2(x, p...)
    ex = esc(p[end])
    for i = length(p)-1:-1:1
        ex = :( $(esc(p[i])) + x * $ex )
    end
    ex
end

```

 ![3](https://global.discourse-cdn.com/julialang/original/3X/d/1/d1bac82bdc88e0be5cdee3aff0b91daf25258b18.png)

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [January 10, 2018, 1:04am UTC](https://discourse.julialang.org/t/some-queries-about-macro-horner/8258/2 "2018-01-10T01:04:29Z")

</div>

Try replacing `x` with an expression, and you’ll see that `horner2` re-computes that expression every time instead of re-using its value (you’re also missing an `$(esc(x))` in your code):

```julia
julia> macro horner2(x, p...)
           ex = esc(p[end])
           for i = length(p)-1:-1:1
               ex = :( $(esc(p[i])) + $(esc(x)) * $ex )
           end
           ex
       end
@horner2 (macro with 1 method)

julia> macroexpand(:(@horner2(expensive_function(), 2, 3, 4)))
:(2 + expensive_function() * (3 + expensive_function() * 4))

```

compare with:

```julia
julia> macroexpand(:(@horner(expensive_function(), 2, 3, 4)))
quote
    #2#t = expensive_function()
    2 + #2#t * (3 + #2#t * 4)
end
```

---

<div class="post-metadata">

**Author:** ![Sabodhapati](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sabodhapati/32/2782_2.png) [@Sabodhapati](https://discourse.julialang.org/u/Sabodhapati)\
**Post date:** [January 10, 2018, 2:38am UTC](https://discourse.julialang.org/t/some-queries-about-macro-horner/8258/3 "2018-01-10T02:38:57Z")

</div>

wow, thank you very much!
