# Seven Lines of Julia (examples sought)

**URL:** <https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416>\
**Category:** General Usage\
**Created:** [November 19, 2020, 7:37am UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416 "2020-11-19T07:37:29Z")\
**Posts on this page:** 1\
**Showing post:** 144

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [February 13, 2022, 4:34pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/144 "2022-02-13T16:34:34Z")

</div>

My current favourite

```julia
using ForwardDiff, ReverseDiff
function callprice(r, T, σ, S₀, K; n = 100)
    memo = Dict(); dt = T / n; u = exp(σ * √dt); q = (exp(r * dt) - 1/u) / (u - 1/u)
    function helper(i, pos)
        get!(memo, (i, pos)) do
            if i == n; max(S₀ * u^pos - K, 0) else exp(- r * dt) * (q*helper(i+1, pos+1) + (1-q)*helper(i+1, pos-1)) end end end
    helper(0, 0) end
(ForwardDiff.derivative(s -> callprice(r, T, σ, s, K), S₀), ReverseDiff.gradient(s -> callprice(r, T, σ, s[1], K), [S₀]))

```

Automatic differentiation through a memoized recursive function … would probably not even have tried that in any other language.  
Unfortunately, I did not get it to work when using an array instead of a dictionary as the memo-table. Maybe I should have another look at Zygote buffers …

---

_[View the full topic](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416)._
