# Recursion in Julia — bad idea?

**URL:** https://discourse.julialang.org/t/recursion-in-julia-bad-idea/99304
**Category:** Performance
**Tags:** recursion
**Created:** [May 23, 2023, 9:16pm UTC](https://discourse.julialang.org/t/recursion-in-julia-bad-idea/99304 "2023-05-23T21:16:58Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![rgu](https://avatars.discourse-cdn.com/v4/letter/r/cab0a1/32.png) [@rgu](https://discourse.julialang.org/u/rgu)
#### Post date: [May 23, 2023, 9:16pm UTC](https://discourse.julialang.org/t/recursion-in-julia-bad-idea/99304/1 "2023-05-23T21:16:58Z")

</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 sure it is not the case of a bug in DFS itself).  
I found some articles, where explaned that Julia is just [bad in recursion](https://craftofcoding.wordpress.com/2021/02/12/julia-is-bad-for-recursion/).

Is that true and Julia is wrong tool for such tasks? I know that there is recursion-free variant for DFS using stack (may be I will implement it too).

btw, is there any good intro to the best practice where/how to use Julia?  
Many thanks!

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [May 23, 2023, 9:27pm UTC](https://discourse.julialang.org/t/recursion-in-julia-bad-idea/99304/2 "2023-05-23T21:27:57Z")

</div>

It’s not so much that Julia is the wrong tool, but that recursion in Julia is the wrong tool. You can always rewrite your recursion by pushing and popping from a stack, and you know better than the compiler what data you care about. With the recursive solution, the compiler needs to keep around a bunch of extra information about what functions were called in order for `return` to go to the right place.

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [May 23, 2023, 9:28pm UTC](https://discourse.julialang.org/t/recursion-in-julia-bad-idea/99304/3 "2023-05-23T21:28:00Z")

</div>

Hi @rgu, welcome aboard!  
Julia can do recursion without major issue, the trouble (as you noted) is the stack depth. A DFS on a 1000x1000 grid means you might have as many as 1 000 000 stacked recursive calls, which is a lot. I would definitely recommend you switch to a loop-based implementation (or borrow one from Graphs.jl using GridGraphs.jl, if that is appropriate for your problem?).  
As for resources on “best Julia practices”, not sure what you expect, can you clarify? Do you want some form of decision chart on when to use Python vs Julia for instance? Cause that is hard to provide, and it depends a lot on personal preferences. If you tell us more about yourself or your problem(s) we might be able to help

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [May 23, 2023, 11:39pm UTC](https://discourse.julialang.org/t/recursion-in-julia-bad-idea/99304/4 "2023-05-23T23:39:34Z")

</div>

To be honest, I think that blog post makes a terrible job of explaining why Julia would struggle with recursion, and to compare its limitations to other languages (what is `Slowsort`? where is its code in both languages? why define a _depth of recursion before failure_ metric and just use it for Julia?).

The stack limitation exists for any languages, just some languages are better at reorganizing some specific kinds of recursion (not all types of recursion are automatically optimized) into the loop with manually managed stack that you can always code yourself.

> If you are thinking about using Julia to implement recursive algorithms, forget it. It just won’t work properly, well not unless you have some idea what the depth of recursion will be… but that’s hardly the point of recursion.

This is a valid comment for all languages, I do not understand why to single out Julia here.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [May 24, 2023, 12:35am UTC](https://discourse.julialang.org/t/recursion-in-julia-bad-idea/99304/5 "2023-05-24T00:35:38Z")

</div>

On the contrary, I think recursion is a great tool and use it often in Julia; of course, it has its limits, but no more so than in most other performance-oriented imperative languages (e.g. C).

For example, it is a great way to exploit memory locality in data structures, as I [demo’ed for matrix multiplication](https://discourse.julialang.org/t/julia-matrix-multiplication-performance/55175/12) for example (or for [FFTs](https://www.fftw.org/)). It can also be a nice way to express multidimensional algorithms, e.g. for [multidimensional Chebyshev interpolation](https://github.com/JuliaMath/FastChebInterp.jl/blob/d9330988e6f23f929e99c30eb597e69a6f67dd5e/src/eval.jl#L16-L56) or for [multidimensional in-place array reversal](https://github.com/JuliaLang/julia/pull/37367). Julia’s built-in `sum` function [uses recursion](https://github.com/JuliaLang/julia/blob/c470dc369865cf2f90ad34e25e110b72dd7fbd87/base/reduce.jl#L251-L275) to implement [pairwise summation](https://en.wikipedia.org/wiki/Pairwise_summation) for greater floating-point accuracy. And, of course, recursive sorting algorithms like [Quicksort](https://en.wikipedia.org/wiki/Quicksort) and [Merge sort](https://en.wikipedia.org/wiki/Merge_sort) are famous, and Julia’s `sort` function has both [recursive quicksort](https://github.com/JuliaLang/julia/blob/c470dc369865cf2f90ad34e25e110b72dd7fbd87/base/sort.jl#L2021-L2037) and [recursive mergesort](https://github.com/JuliaLang/julia/blob/c470dc369865cf2f90ad34e25e110b72dd7fbd87/base/sort.jl#L2039-L2081).

But you have to use recursion appropriately. If you are recursing thousands of times in an [imperative language](https://en.wikipedia.org/wiki/Imperative_programming), you should probably consider a loop (perhaps with an explicit stack/queue data structure). If each recursive call is extremely cheap, then you should consider “coarsening” your recursion (e.g. by enlarging the base case) to amortize the function-call overhead (this is true in any language, BTW, at least for non-tail calls). See also the discussion here: [Recursive call vs while loop - #18 by stevengj](https://discourse.julialang.org/t/recursive-call-vs-while-loop/7723/18)

> [@Oscar\_Smith](#):
>
> It’s not so much that Julia is the wrong tool, but that recursion in Julia is the wrong tool.

I think it’s a common myth that recursion is necessarily slow. The key trick that is usually missing from such discussions is to enlarge the base case.

---

<div class="post-metadata">

### Author: ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)
#### Post date: [May 24, 2023, 1:49am UTC](https://discourse.julialang.org/t/recursion-in-julia-bad-idea/99304/6 "2023-05-24T01:49:44Z")

</div>

> [@Henrique\_Becker](#):
>
> This is a valid comment for all languages, I do not understand why to single out Julia here.

Yes, I routinely interact with people who’ve hit recursion depth limits in Python. This is just something people eventually need to learn to handle in whatever language they use.

---

<div class="post-metadata">

### Author: ![rgu](https://avatars.discourse-cdn.com/v4/letter/r/cab0a1/32.png) [@rgu](https://discourse.julialang.org/u/rgu)
#### Post date: [May 24, 2023, 8:19am UTC](https://discourse.julialang.org/t/recursion-in-julia-bad-idea/99304/7 "2023-05-24T08:19:05Z")

</div>

Well, I try to expand. I am a teacher in high school, quite clever kids of age about 13-17.  
We use Python and C++.  
Python — as a wide-range tool (to be able to write standard algorithms like sorting or Eratosphenes sieve as well as some image processing, ML, etc)  
Kids who want to write more effective (fast) programs and participate in competitive programming contests use C++.  
May be I am speaking of stereotypes, but **usually** we wouldn’t use C++ or Fortran for UI or Python for low-level system utilities.

I’ve read some materials on statistics with R and Julia and want to refresh my language base. So started Julia and try to implement different tasks using it (and thereby learn the basics). From other point of view — it is important for me to understand what is good task for Julia and what is not. For example, is that good idea to implement compression using Huffmann code or cryptographic protocol (RSA) or computational geometry to learn core 3d-graphics. I believe there are proper libraries in Julia to do all this stuff. Idea is to do this ourselves to know how-it-works.

Comparing Julia vs Python (I’m afraid because started Julia week ago)… it seems Julia needs a bit more qualification from programmer and doesn’t provide simple workaround without real understanding of what you do. But “it’s not a bug it’s a feature”, of course.

---

<div class="post-metadata">

### Author: ![rgu](https://avatars.discourse-cdn.com/v4/letter/r/cab0a1/32.png) [@rgu](https://discourse.julialang.org/u/rgu)
#### Post date: [May 24, 2023, 8:26am UTC](https://discourse.julialang.org/t/recursion-in-julia-bad-idea/99304/8 "2023-05-24T08:26:42Z")

</div>

Exactly the same — it is standard error I see in students code when they got Run-Time with clear DFS and I know that setrecursionlimit is a common practice here.  
I just realized that in Julia workaround is to use stack instead. That is much harder task (for students) but it is honest at least, I agree.

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [May 24, 2023, 10:14am UTC](https://discourse.julialang.org/t/recursion-in-julia-bad-idea/99304/9 "2023-05-24T10:14:23Z")

</div>

I think at the high school level, whatever you want to do in Python you can do in Julia, and vice versa. Their syntax is very similar, and all the bases are covered on either side. In my opinion, Julia might actually be a nice language for learning programming, since you can poke around as deep as you want to understand stuff.  
As far as speed is concerned, Julia _can_ rival C++ in many cases, but it takes some work to get it there. However, I would argue the amount of work needed is much less than learning C++, so it may be a good choice for competitive programming as well! Take a look at [Which programming language is fastest? (Benchmarks Game)](https://benchmarksgame-team.pages.debian.net/benchmarksgame/index.html) for instance

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [May 24, 2023, 11:51am UTC](https://discourse.julialang.org/t/recursion-in-julia-bad-idea/99304/10 "2023-05-24T11:51:00Z")

</div>

I think Juila is a very interesting language for teaching programming. Python of course gives you, with the libraries, the easiest path for students to code anything and produce something pretty. But if your students have already the prospect of using low level languages for anything, they are interested in really learning programming. In that case, Julia is a good fit, because it can expose to the user many fundamental concepts that python will mostly hide. In terms of performance, basically the students have to learn that querying new memory addresses and slots to the system is slow. Most performance tips of Julia derive from that: preallocate memory, avoid intermediates, avoid type instability (which imply boxing and, thus allocations), avoid dynamic dispatch.

Also with Julia they learn what compilation means (which python also hides), learns what code specialization is, can easily play with different number representations in simple computations and check for the performance.

We have students (at the university) that, on the contrary have been so much influenced by the python way of thinking that one needs to break a learning barrier to introduce the concepts that are necessary for performance programming, in any language. Everything just seem magical.

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [May 24, 2023, 12:26pm UTC](https://discourse.julialang.org/t/recursion-in-julia-bad-idea/99304/11 "2023-05-24T12:26:47Z")

</div>

To add on this:  
Learning recursion without hitting the stack limit is NOT learning recursion.

Learning about algorithms and programming techniques should include the underlying principles of how processors and memory management work. Recursion and the stack is an example on why this is important. Another examples: Integer operations and overflow, float operations and precision, there are many more.

Even if modern software development doesn’t need to use registers and memory addresses anymore, it is good for a general understanding of software, to know what’s going on in the CPU and in the memory. Basic data types for example do have a correspondence in the memory architecture of the machines. This makes it easier to understand why there is something like an Int64. The binary representation of numbers in computers, as an extreme example, is not a mandatory knowledge for many programming tasks, but it should be definitely known, as, I guess, many would agree.

Therefor: Julia is perfect for learning recursion. Next level: perfect for learning how to do the same in loops. Next level: perfect to learn about tail recursion and it’s optimization.

That would bring the principles into relation to each other and in relation to the hardware. Those relations makes it easier(!) to learn, comprehend and remember them.

---

<div class="post-metadata">

### Author: ![Tarny\_GG\_Channie](https://avatars.discourse-cdn.com/v4/letter/t/3bc359/32.png) [@Tarny\_GG\_Channie](https://discourse.julialang.org/u/Tarny_GG_Channie)
#### Post date: [May 24, 2023, 12:36pm UTC](https://discourse.julialang.org/t/recursion-in-julia-bad-idea/99304/12 "2023-05-24T12:36:28Z")

</div>

Recursion is not an efficient way to implement DFS no matter what language you use. This is not a Julia issue. It’s an issue with implementing DFS with recursion in general.

That being said, functional programmers may have some tricks to resolve this issue. For Julia, it is best to just turn to stack.

---

<div class="post-metadata">

### Author: ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)
#### Post date: [May 24, 2023, 1:00pm UTC](https://discourse.julialang.org/t/recursion-in-julia-bad-idea/99304/13 "2023-05-24T13:00:26Z")

</div>

I solved some Project Euler stuff using Scheme, and started to like tail recursion, so when I found out that Julia [doesn’t (yet?) support tail recursion](https://discourse.julialang.org/t/does-julia-have-tail-call-optimization/64101), I was a little bummed. Mind you, I wasn’t bummed because lack of TCO was hurting my ability to work, or that I had run into a limit somewhere … I just missed it.

I think I just got allergic to `for` loops, and tried to solve everything in a Scheme-y way in any language that would allow it. Julia is the first (non-`C`) language I have used on a regular basis where `for` loops are not a bad idea.

Edit, and unrelated, but [Project Euler](https://projecteuler.net/) is a lot of fun. I might try it again with Julia.

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [May 24, 2023, 1:26pm UTC](https://discourse.julialang.org/t/recursion-in-julia-bad-idea/99304/14 "2023-05-24T13:26:22Z")

</div>

> [@oheil](#):
>
> To add on this:  
> Learning recursion without hitting the stack limit is NOT learning recursion.

For recursion in programming, yes, I do agree. For recursion in mathematics, of course not. If I was a Maths teacher just using a language to exemplify some concepts, I would not teach about the stack limit.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [March 21, 2024, 11:38am UTC](https://discourse.julialang.org/t/recursion-in-julia-bad-idea/99304/15 "2024-03-21T11:38:46Z")

</div>

5 posts were split to a new topic: [Memory allocation in recursion](https://discourse.julialang.org/t/memory-allocation-in-recursion/111929)
