# \#recursion

**URL:** https://discourse.julialang.org/tag/recursion/862.md

[Latest](https://discourse.julialang.org/latest.md) · [Categories](https://discourse.julialang.org/categories.md) · [Tags](https://discourse.julialang.org/tags.md)

---

## [Why is Julia the only one that doesn't overflow?](https://discourse.julialang.org/t/why-is-julia-the-only-one-that-doesnt-overflow/135758)

<div class="topic-metadata">

**Author:** [@cocoa1231](https://discourse.julialang.org/u/cocoa1231)\
**Replies:** 5\
**Last updated:** [February 20, 2026, 7:42pm UTC](https://discourse.julialang.org/t/why-is-julia-the-only-one-that-doesnt-overflow/135758 "2026-02-20T19:42:37Z")

</div>

I have the following tail recursive fibonacci function in zig, rust, julia and c. For some reason, only the Julia version doesn’t overflow. Why is that? fn fib(n: usize, p1: usize, p2: usize) -\> usize { if n \< 2 { …

---

## [\[ANN\] RecursiveFactorization.jl](https://discourse.julialang.org/t/ann-recursivefactorization-jl/39213)

<div class="topic-metadata">

**Author:** [@YingboMa](https://discourse.julialang.org/u/YingboMa)\
**Replies:** 14\
**Last updated:** [February 13, 2026, 5:40pm UTC](https://discourse.julialang.org/t/ann-recursivefactorization-jl/39213 "2026-02-13T17:40:28Z")

</div>

RecursiveFactorization allows you to perform any factorization you’d like so long as it is lu. If you like LU, DifferentialEquations.jl, or recursion, you’ll love RecursiveFactorization.jl. DifferentialEquations.jl use…

---

## [What is the status of type inference of recursive calls? Odd statefulness?](https://discourse.julialang.org/t/what-is-the-status-of-type-inference-of-recursive-calls-odd-statefulness/132545)

<div class="topic-metadata">

**Author:** [@Benny](https://discourse.julialang.org/u/Benny)\
**Replies:** 4\
**Last updated:** [September 26, 2025, 7:09pm UTC](https://discourse.julialang.org/t/what-is-the-status-of-type-inference-of-recursive-calls-odd-statefulness/132545 "2025-09-26T19:09:15Z")

</div>

Evidently, the situation has improved from v1.10.10 because the examples from the links below on v1.11.7 are inferred with concrete return types and pass JET.@report\_opt. Note that type instabilities that don’t cause int…

---

## [Efficient tuple concatenation](https://discourse.julialang.org/t/efficient-tuple-concatenation/5398)

<div class="topic-metadata">

**Author:** [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)\
**Replies:** 16\
**Last updated:** [January 17, 2025, 7:42pm UTC](https://discourse.julialang.org/t/efficient-tuple-concatenation/5398 "2025-01-17T19:42:51Z")

</div>

I was needing a way to concatenate an undefined number of tuples. I came up with tuplejoin(t1::Tuple, t2::Tuple, t3...) = tuplejoin((t1..., t2...), t3...) tuplejoin(t::Tuple) = t This works julia\> tuplejoin((1,2),(3,4…

---

## [Recursion base vs hand-made recursion](https://discourse.julialang.org/t/recursion-base-vs-hand-made-recursion/124235)

<div class="topic-metadata">

**Author:** [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Replies:** 13\
**Last updated:** [December 29, 2024, 6:07pm UTC](https://discourse.julialang.org/t/recursion-base-vs-hand-made-recursion/124235 "2024-12-29T18:07:17Z")

</div>

Trying to solve the problem d17 of this year’s AOC, I found that, among the proposed solutions, the most efficient seems to be the one that uses recursion. for example, compared to an iterative one that works on the sam…

---

## [Why does it matter where I place return in above code?](https://discourse.julialang.org/t/why-does-it-matter-where-i-place-return-in-above-code/121484)

<div class="topic-metadata">

**Author:** [@matic\_lauko](https://discourse.julialang.org/u/matic_lauko)\
**Replies:** 8\
**Last updated:** [October 20, 2024, 4:55pm UTC](https://discourse.julialang.org/t/why-does-it-matter-where-i-place-return-in-above-code/121484 "2024-10-20T16:55:36Z")

</div>

I’ve tried making a simple Greatest Common Denominator code but output differs depending on where I use return. First is the code that works as intendent. By using else statement, as far as I understand return runs only…

---

## [Can I use \`pmap\` on a recursive program?](https://discourse.julialang.org/t/can-i-use-pmap-on-a-recursive-program/114684)

<div class="topic-metadata">

**Author:** [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Replies:** 1\
**Last updated:** [May 25, 2024, 6:46am UTC](https://discourse.julialang.org/t/can-i-use-pmap-on-a-recursive-program/114684 "2024-05-25T06:46:16Z")

</div>

Can I use pmap on a recursive program? Take the following recursive function as an example: using Distributed addprocs(3) function mymax(x) n = length(x) if n == 1 x\[1\] else y = pmap(mymax, \[…

---

## [Compiler recursion limit](https://discourse.julialang.org/t/compiler-recursion-limit/112698)

<div class="topic-metadata">

**Author:** [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Replies:** 10\
**Last updated:** [April 17, 2024, 11:52pm UTC](https://discourse.julialang.org/t/compiler-recursion-limit/112698 "2024-04-17T23:52:55Z")

</div>

I’ve written some recursive code that compiles away due to (I believe) constant propagation. However, I’m afraid that if I run the code on a larger problem I will hit a compiler recursion limit and it will no longer comp…

---

## [Memory allocation in recursion](https://discourse.julialang.org/t/memory-allocation-in-recursion/111929)

<div class="topic-metadata">

**Author:** [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Replies:** 11\
**Last updated:** [March 21, 2024, 8:08pm UTC](https://discourse.julialang.org/t/memory-allocation-in-recursion/111929 "2024-03-21T20:08:27Z")

</div>

Does Julia allocate memory when recursing? I see a ton of memory allocation in a recursive quicksort…

---

## [Recursion in Julia — bad idea?](https://discourse.julialang.org/t/recursion-in-julia-bad-idea/99304)

<div class="topic-metadata">

**Author:** [@rgu](https://discourse.julialang.org/u/rgu)\
**Replies:** 13\
**Last updated:** [May 24, 2023, 1:26pm UTC](https://discourse.julialang.org/t/recursion-in-julia-bad-idea/99304 "2023-05-24T13:26:22Z")

</div>

I’m trying to learn Julia on examples. For now I’m trying to solve some problem with DFS as a core algorithm in my solution. This recursive DFS is running on a max 1000×1000 grid and I’ve got StackOverFlow (I’m pretty s…

---

## [Make Julia complete the inference of some recursive code](https://discourse.julialang.org/t/make-julia-complete-the-inference-of-some-recursive-code/111449)

<div class="topic-metadata">

**Author:** [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Replies:** 2\
**Last updated:** [March 11, 2024, 2:44pm UTC](https://discourse.julialang.org/t/make-julia-complete-the-inference-of-some-recursive-code/111449 "2024-03-11T14:44:47Z")

</div>

Background I have a function with a real number input coming from a finite interval. To implement the function, I break down that interval into several subintervals and implement the function on each of them. Suppose, fo…

---

## [Why are self-referential struct parameters impossible?](https://discourse.julialang.org/t/why-are-self-referential-struct-parameters-impossible/110982)

<div class="topic-metadata">

**Author:** [@mnemnion](https://discourse.julialang.org/u/mnemnion)\
**Replies:** 19\
**Last updated:** [March 2, 2024, 11:46pm UTC](https://discourse.julialang.org/t/why-are-self-referential-struct-parameters-impossible/110982 "2024-03-02T23:46:18Z")

</div>

Neither of these compiles: struct Tree{V\<:Any,T\<:Union{Tree,Nothing}} value::V left::T right::T end struct Tree{V,T} where {V\<:Any,T\<:Union{Tree,Nothing}} value::V left::T right::T end It m…

---

## [Julia never stops compiling for new recursive Vector types](https://discourse.julialang.org/t/julia-never-stops-compiling-for-new-recursive-vector-types/110232)

<div class="topic-metadata">

**Author:** [@mnemnion](https://discourse.julialang.org/u/mnemnion)\
**Replies:** 18\
**Last updated:** [February 15, 2024, 8:21pm UTC](https://discourse.julialang.org/t/julia-never-stops-compiling-for-new-recursive-vector-types/110232 "2024-02-15T20:21:54Z")

</div>

This is… unexpected behavior. I feel like inference should give up at some point. julia\> w = \[1\]; @time for i = 1:100 global w = \[w\] end 0.227547 seconds (466.23 k allocations: 31.348 MiB, 1.52% gc tim…

---

## [Dictionary of dictionaries as decision tree](https://discourse.julialang.org/t/dictionary-of-dictionaries-as-decision-tree/108783)

<div class="topic-metadata">

**Author:** [@leoflotor](https://discourse.julialang.org/u/leoflotor)\
**Replies:** 3\
**Last updated:** [January 14, 2024, 5:33pm UTC](https://discourse.julialang.org/t/dictionary-of-dictionaries-as-decision-tree/108783 "2024-01-14T17:33:22Z")

</div>

Hi! I am trying to do a decision tree using my implementation of ID3 but I am having some problems with one of my functions. For example, I have the following data frame: julia\> df 14×6 DataFrame Row │ pa as …

---

## [How to return self recursive function?](https://discourse.julialang.org/t/how-to-return-self-recursive-function/108564)

<div class="topic-metadata">

**Author:** [@AwesomeQuest](https://discourse.julialang.org/u/AwesomeQuest)\
**Replies:** 8\
**Last updated:** [January 10, 2024, 11:57am UTC](https://discourse.julialang.org/t/how-to-return-self-recursive-function/108564 "2024-01-10T11:57:17Z")

</div>

I want to make a function that takes a function and vector of initial conditions as arguments. function recseq( a::T, init::AbstractVector) where{T\<:Function} return n-\> begin if(n in init) return init\[n+1\] e…

---

## [Recursion and while loops: pros and cons](https://discourse.julialang.org/t/recursion-and-while-loops-pros-and-cons/106559)

<div class="topic-metadata">

**Author:** [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Replies:** 4\
**Last updated:** [November 22, 2023, 9:22am UTC](https://discourse.julialang.org/t/recursion-and-while-loops-pros-and-cons/106559 "2023-11-22T09:22:46Z")

</div>

This recursive function: function foo1(x) y = someoperations(y) if somecondition(y) return y else return foo1(y) end end does essentially the same as with the loop: function foo2(x) …

---

## [\[ANN\] TupleSorting: Sort tuples efficiently and with good type inference](https://discourse.julialang.org/t/ann-tuplesorting-sort-tuples-efficiently-and-with-good-type-inference/105756)

<div class="topic-metadata">

**Author:** [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Replies:** 3\
**Last updated:** [November 15, 2023, 9:51am UTC](https://discourse.julialang.org/t/ann-tuplesorting-sort-tuples-efficiently-and-with-good-type-inference/105756 "2023-11-15T09:51:03Z")

</div>

TupleSorting is being registered. Use it to sort tuples (even large ones) efficiently and with good type inference: julia\> using TupleSorting julia\> const TS = TupleSorting TupleSorting julia\> t = ntuple((\_ -\> rand(0:…

---

## [ANN: TupleShuffling: shuffling of tuples and generating random permutation tuples](https://discourse.julialang.org/t/ann-tupleshuffling-shuffling-of-tuples-and-generating-random-permutation-tuples/105506)

<div class="topic-metadata">

**Author:** [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Replies:** 3\
**Last updated:** [October 28, 2023, 6:47pm UTC](https://discourse.julialang.org/t/ann-tupleshuffling-shuffling-of-tuples-and-generating-random-permutation-tuples/105506 "2023-10-28T18:47:11Z")

</div>

TupleShuffling is being registered. Use it when: There’s a tuple and you want to shuffle its elements. So like shuffle from the standard library, but for tuples instead of for arrays You want a random permutation of st…

---

## [Why do these allocate?!](https://discourse.julialang.org/t/why-do-these-allocate/105497)

<div class="topic-metadata">

**Author:** [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Replies:** 4\
**Last updated:** [October 28, 2023, 12:58pm UTC](https://discourse.julialang.org/t/why-do-these-allocate/105497 "2023-10-28T12:58:36Z")

</div>

The functions shuf1 and shuf2 are two different implementations of a Fisher-Yates shuffle for tuples. Why do both allocate, even in trivial cases? The recursion depth should be small and a compile-time constant and all t…

---

## [Any good resource to understand the issues with recursion in Julia?](https://discourse.julialang.org/t/any-good-resource-to-understand-the-issues-with-recursion-in-julia/104615)

<div class="topic-metadata">

**Author:** [@bguillen](https://discourse.julialang.org/u/bguillen)\
**Replies:** 11\
**Last updated:** [October 6, 2023, 11:20am UTC](https://discourse.julialang.org/t/any-good-resource-to-understand-the-issues-with-recursion-in-julia/104615 "2023-10-06T11:20:49Z")

</div>

I’m implementing basically the Median of Medians (actually, Median of Ninthers) algorithm for quick deterministic selection (see \[original post\]). It is recursive, as in the way to find a pivot is by calculating the medi…

---

## [How are (mutually) recursive methods inferred correctly?](https://discourse.julialang.org/t/how-are-mutually-recursive-methods-inferred-correctly/101463)

<div class="topic-metadata">

**Author:** [@Benny](https://discourse.julialang.org/u/Benny)\
**Replies:** 3\
**Last updated:** [July 11, 2023, 7:02am UTC](https://discourse.julialang.org/t/how-are-mutually-recursive-methods-inferred-correctly/101463 "2023-07-11T07:02:20Z")

</div>

This is something I took for granted and never really thought of. A C example I pulled from wikipedia will help me explain: # really inefficient check of evenness or oddness # "is n even" -\> "is n-1 odd" -\> "is n-2 even…

---

## [Type-stability of recursive functions](https://discourse.julialang.org/t/type-stability-of-recursive-functions/78946)

<div class="topic-metadata">

**Author:** [@nchisholm](https://discourse.julialang.org/u/nchisholm)\
**Replies:** 6\
**Last updated:** [May 1, 2023, 1:05pm UTC](https://discourse.julialang.org/t/type-stability-of-recursive-functions/78946 "2023-05-01T13:05:12Z")

</div>

I have challenged myself to write a (ideally type-stable) function that computes the determinant of (small) matricies using the O(n!) recursive expansion formula without resorting to @generated. I ended up with the fol…

---

## [How to call a recursive function without stackoverflow?](https://discourse.julialang.org/t/how-to-call-a-recursive-function-without-stackoverflow/95865)

<div class="topic-metadata">

**Author:** [@AwesomeQuest](https://discourse.julialang.org/u/AwesomeQuest)\
**Replies:** 10\
**Last updated:** [March 11, 2023, 5:11pm UTC](https://discourse.julialang.org/t/how-to-call-a-recursive-function-without-stackoverflow/95865 "2023-03-11T17:11:41Z")

</div>

When calling julia\> f(x) = x\>1 ? √(6+f(x-1)) : 1 f (generic function with 1 method) julia\> f(104475) 3.0 julia\> f(104476) ERROR: StackOverflowError: Stacktrace: \[1\] \< @ .\\REPL\[76\]:1 \[inlined\] \[2\] \> @ .\\operato…

---

## [Best Way to Break out of Recursive Loop in Julia](https://discourse.julialang.org/t/best-way-to-break-out-of-recursive-loop-in-julia/95653)

<div class="topic-metadata">

**Author:** [@phantom](https://discourse.julialang.org/u/phantom)\
**Replies:** 4\
**Last updated:** [March 8, 2023, 10:07am UTC](https://discourse.julialang.org/t/best-way-to-break-out-of-recursive-loop-in-julia/95653 "2023-03-08T10:07:07Z")

</div>

This is probably not a great example of recursion but suppose I have a recursive function, usually in a loop I can break out of the first layer with something simple like function check(stuff) cont = Ref(true) @…

---

## [Best way to break out of recursive loop in Julia?](https://discourse.julialang.org/t/best-way-to-break-out-of-recursive-loop-in-julia/95652)

<div class="topic-metadata">

**Author:** [@phantom](https://discourse.julialang.org/u/phantom)\
**Replies:** 0\
**Last updated:** [March 7, 2023, 8:22am UTC](https://discourse.julialang.org/t/best-way-to-break-out-of-recursive-loop-in-julia/95652 "2023-03-07T08:22:14Z")

</div>

(topic deleted by author)

---

## [Performant Recursive Anonymous Functions](https://discourse.julialang.org/t/performant-recursive-anonymous-functions/90984)

<div class="topic-metadata">

**Author:** [@uniment](https://discourse.julialang.org/u/uniment)\
**Replies:** 23\
**Last updated:** [November 30, 2022, 11:01pm UTC](https://discourse.julialang.org/t/performant-recursive-anonymous-functions/90984 "2022-11-30T23:01:28Z")

</div>

What’s the preferred way to write recursive anonymous functions? Consider a demo function: julia\> function fib(n) n ≤ 1 && return n return fib(n-1) + fib(n-2) end fib (generic function with…

---

## [Unconventional use of \`merge\`](https://discourse.julialang.org/t/unconventional-use-of-merge/85472)

<div class="topic-metadata">

**Author:** [@danielsoutar](https://discourse.julialang.org/u/danielsoutar)\
**Replies:** 2\
**Last updated:** [August 8, 2022, 2:40pm UTC](https://discourse.julialang.org/t/unconventional-use-of-merge/85472 "2022-08-08T14:40:57Z")

</div>

TL;DR: I understand at a high level what this code is doing, but the implementation is very unusual and I don’t understand what is going on. I have the following the code that someone else wrote: """ deep\_merge(d..…

---

## [Performance of recursive function](https://discourse.julialang.org/t/performance-of-recursive-function/83961)

<div class="topic-metadata">

**Author:** [@Fabrice\_Rosay](https://discourse.julialang.org/u/Fabrice_Rosay)\
**Replies:** 12\
**Last updated:** [July 10, 2022, 7:58am UTC](https://discourse.julialang.org/t/performance-of-recursive-function/83961 "2022-07-10T07:58:48Z")

</div>

Hi, I have the following recursive function to calculate the number of positions in a game. All functions inside perft (play, gen\_moves, isOver) are non allocating, yet perft allocates a lot of memory. Using the track…

---

## [How to save recursive functions in julia](https://discourse.julialang.org/t/how-to-save-recursive-functions-in-julia/83644)

<div class="topic-metadata">

**Author:** [@ArjunSingh26](https://discourse.julialang.org/u/ArjunSingh26)\
**Replies:** 1\
**Last updated:** [July 1, 2022, 9:31pm UTC](https://discourse.julialang.org/t/how-to-save-recursive-functions-in-julia/83644 "2022-07-01T21:31:22Z")

</div>

Hi everyone, This summer, I’m doing some research involving mathematical particle simulations. Part of my work revolves around a “shape function”, which for the purposes of this post, is simply a function that takes an …

---

## [Yet another TCO thread](https://discourse.julialang.org/t/yet-another-tco-thread/81146)

<div class="topic-metadata">

**Author:** [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Replies:** 5\
**Last updated:** [May 25, 2022, 6:06am UTC](https://discourse.julialang.org/t/yet-another-tco-thread/81146 "2022-05-25T06:06:53Z")

</div>

Also, AFAIK, Julia doesn’t implement the tail-call optimization yet, which is crucial for the performance of recursive functions.

[Next page](https://discourse.julialang.org/tag/recursion/862.md?match_all_tags=true&page=1&tags%5B%5D=recursion)
