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

2

u/dizzyflames Apr 05 '22

Recursion is weird to me. I kind of understand it but still feel like I don't. You give me a recursion problem and I won't know if it works until I compile it even if it's a pretty basic problem but I still somehow get it working even if I don't know how I did it.

0

u/[deleted] Apr 05 '22

I came up with this explanation in case I’m asked at an interview, so you will be my test monkey for it lol.

Imagine you have a tower of lego. Literally just bricks stacked onto one another vertically. And you decide you want to take it apart.

Your plan is to break off one piece at a time. So you start from the very top, break the topmost piece, and put it on the floor to your left. Now you are left with the tower that has one less pieces. You break off the new top piece and put it aside. You basically repeat this, until your tower is just one lego brick (this is the base condition). When its just one brick left, you put it with the others, and voila, you just recursively dismantled a lego tower. The return part of recursion just means that you are passing the last value up, so the originally called instance of the function receives it.

If we translate into code we get function breakTower(tower) { if(tower.size == 1) { return tower.top } else { tower.breakTopOff() return breakTower(tower) } }

1

u/Liambass Apr 07 '22 edited Apr 07 '22

That description seems more like an iterative approach to me?

"remove bricks until there aren't any more".

I think a better example would be trying to disassemble the tower from the bottom;

void breakTower(Tower tower) {
    breakTowerRecursive(tower, tower.base);
}

void breakTowerRecursive(Tower tower, Brick brick){
    if (brick.above == null) {
        brick.remove();
    } else {
        breakTower(tower, brick.above);
        brick.remove();

1

u/[deleted] Apr 07 '22

That’s actually a good suggestion. I agree. I would suggest the code however to be two separate functions rather than two overloaded ones, since some people might not be familiar with function overloading and get confused.

P.s I also like your use of a linked list. I was thinking of a vector at first, but linked list makes it clearer.

1

u/Liambass Apr 07 '22

Yeah, that's fair. Let's see if I can't do a wee edit.

1

u/CozyRedBear Apr 05 '22

Recursion can be difficult because it requires you attack a problem from the other way around. Normally you can go headfirst into a solution and arrive at the answer, but recursion takes the answer as the starting point and works its way outward.

1

u/warlaan Apr 06 '22

I find it easier to understand the concept by looking at how to come up with recursive code rather than trying to analyze existing one.

When writing a recursive function you need two parts: one trivial solution and one way to get from the next complicated problem to the trivial one.

For example let's say we want to write a multiply function for positive integers x and y.

The trivial case would be multiplying something with 0. No matter what the other number is, it's trivial to tell the result. So we can immediately say "if the first factor is 0 the result is also 0".

Now what would be the next more complex case? Multiplying something by 1, because that would be one time the other factor more (one time x is x more than zero times x). So the rest of the function would be "otherwise the result is y more than the product of x-1 and y".

That's it, you are done. Even though we only thought about the trivial case and one next case all other cases are automatically handled, because you can simply apply that reduction mechanism several times.

In most situations recursion is not helpful for performance and readability reasons, but on the other hand there are cases where the readability improves drastically by using recursive code.