r/learnprogramming • • Apr 05 '22

Name one programming/comp sci concept you never understood and if you understand it, try to explain it to them

Name a programming concept such as virtual functions, pointers, certain algorithms, etc that you always had a hard time understanding and why.

Someone else who reads yours and does understand it, try to explain it to them to help them learn

1.6k Upvotes

738 comments sorted by

View all comments

Show parent comments

20

u/AlSweigart Author: ATBS Apr 05 '22

I just got done writing an entire book on recursion.

Recursion is tricky, but mostly it's just poorly taught. The main thing that instructors fail to talk about is the call stack. It's the invisible data structure that automatically handles where the execution should return as you call and return from functions.

Once you know that, and you can see that local variables are local to the function call and not just to the function, it becomes a bit easier to see how recursive functions work.

It's tricky though, because you can't point to the call stack in your source code. Without having that explicitly pointed out, recursion just seems like magic. But it isn't magical or somehow more powerful: *literally anything you can do with recursion you can do without recursion by using a loop and a stack. It's just that recursive algorithms use the (unstated, hidden) call stack as their stack.

(I always get people saying, "some things do require recursion" and, no, they don't require it. Here's a non-recursive Python implementation of the famously recursive Ackermann function, and it has the same output as the normal recursive one.)

It's fine to have difficulty with recursion though; recursion is overrated and often a terrible way to write code. The times when recursion is useful is for problems that involve a tree-like structure and some sort of backtracking (maze solving, tower of hanoi, etc). Otherwise, iterative code is much simpler and I think some programmers just use recursion because it makes them feel smart.

6

u/flank-cubey-cube Apr 05 '22

To add on to your last paragraph, recursion also incurs some overhead and isn't the most efficient way to do things. Each function called recursively gets added to the stack frame, and if you have an *extremely* large tree structure, you'll get a stack overflow. The exception to this is if you are able to optimize for tail recursion, because than the compiler will be able optimize it to a loop.

9

u/AlSweigart Author: ATBS Apr 05 '22

Yes, but I always hold off on performance claims until I run code under a profiler. I've had some recursive code end up being faster than an iterative version. (Not sure how that happened.)

Also, to be specific, the stack overflow happens if you have an extremely deep tree. Like, it could be a linked list (which is basically a unary tree) that is a couple thousand nodes long. If you had an extremely large but shallow tree, you won't have a stack overflow.

And many interpreters/compilers don't have tail call optimization, including the main ones for Python, JavaScript, and Java. I hold the opinion that tail call optimization should never be used.

All recursive functions can be implemented iterative using a loop and stack. If you are doing tail recursion, it's because your recursive algorithm doesn't require the call stack. So iteratively you can implement it as a loop and a stack without the stack, so why not just use a loop? Often times you have to mangle the code and decrease readability to make it tail recursive, so I'd just go with a simple loop every time.

I can't think of a tail recursive function that wouldn't be improved by just using a loop.

1

u/flank-cubey-cube Apr 05 '22 edited Apr 05 '22

Yes, but I always hold off on performance claims until I run code under a profiler. I've had some recursive code end up being faster than an iterative version. (Not sure how that happened.)

True!

(For some reason I can't quote on comments besides the 1st one)

And I should have specified that in my comment that that is only true for deep trees. I came across this because I was unit testing a data structure and created a ridiculously large tree.

And your point on tail recursion is so true! The true reason they exist is to impress interviewers.

1

u/Rote515 Apr 05 '22

It's fine to have difficulty with recursion though; recursion is overrated and often a terrible way to write code.

Fucking preach, if college was to be believed I would be writing recursive algorithms every damn day, I haven’t even seen an example of recursion in the code base I work in…(to be fair I work in a weird as fuck codebase)