Episode notes
Patrick and Jason break down recursion as a practical problem-solving technique rather than a classroom trick. They cover base cases, recursive steps, common pitfalls such as nontermination and stack limits, and real applications in trees, graphs, and divide-and-conquer algorithms.
Chapters
Tap a chapter to play from there.
Transcript
Read the transcript · about 16,640 words, follows along as you listen
A:Programming Throwdown Episode 163: Recursion. Take it away, Patrick! I was trying to
B:come up with a good intro topic for the show, and I decided to harass Jason, so I apologize. No, no, I'm just teasing. I want to talk about electric cars. So electric cars have always been about to be a thing. I actually am not strongly opinionated about whether they will be this sort of exponential adoption thing that I feel like a lot of tech people feel—this is like what will happen, like it'll just become more and more inconvenient to go to the gas station; there won't be gas stations, and then overnight you'll blink and everyone will have electric cars. I don't know, not going to get into that per se, but I have noticed a lot more people driving electric cars. So Jason, I were talking about it briefly, but basically, so I drive a plug-in hybrid which I actually really, really like. I drive a Honda version called a Clarity. I don't even think they make it anymore, but what's the
A:plug-in part mean? Yeah, okay, plug-in.
B:Hybrid. So that's great. So a Prius—most people know about a Prius is just like a hybrid. It means that you have a, you know, electric motor to drive your wheels, but you have a gas engine to run effectively as a generator to make the electricity. And in something like a Prius, it just sort of does this all the time, but it has batteries; it can short-term kind of store the energy and deliver. And I might be mistaken—I may have changed over the years about Priuses—but as an abstraction, this will work. So basically, it runs the gas engine and it can store sort of power, but every time you turn your car off and turn it back on, you're sort of more or less starting from scratch, like it doesn't intend to build up a lot of energy, so it doesn't have big batteries because batteries are pretty expensive. They're also pretty heavy. And so your engine will run from time to time, so there's not really an expected mode of operation where you would go on an entire trip and not use your engine. And so it's sort of more along the spectrum of like even modern cars when you stop at a stoplight will just turn off the engine and has like a little bit of—I actually don't know how they did it, but like, you know, a little bit of it can kind of start quickly and get you going without you kind of noticing. So it's just a more extreme version of that, although it predated it. And then it kind of
A:reminds me of the Formula One cars. Like the Formula One cars have a battery, and whenever you hit the brakes, it charges the battery, and so that builds up and builds up, and then when they're ready to pass somebody, they turn on the battery and the motor, and then that makes them super fast.
B:Yeah, so regenerative braking is this like using the motor that's sort of attached to the spinning part of the wheels to charge up to basically act as a generator, which of course pulling the energy there's, you know, conservation of energy kind of thing. Like, you know, charging the battery makes it harder to turn the motor, which makes you slow down. And so you're recapturing some of that energy, which is a really good idea, which is why it's pretty efficient. So the plug-in idea is—like most of the original Priuses, you couldn't like plug them into the wall; wouldn't do anything. There wasn't like a large battery to charge up. Plug-in hybrid sort of is that it's just sort of like an electric car where you can plug it in and you plug it in to charge it up, and there's a battery that lasts some amount of miles. In my car, it's something I forget the range; I think it's like 30 or 40 miles, so most of the commute, you know, like oh, I just go, you know, up to the grocery store, I go to, you know, the office, or whatever. Just like briefly these things are only, you know, for me like seven, eight miles, so I can go there and back and I don't have to use the gas engine at all. So I have a very small gas tank; it's only like seven gallons, I think is what it is, like very, very tiny. Oh wow, and hardly ever use it. Like I go months and months and never hard. And when I get home at night, I plug the car in—the plug-in hybrid—but if I'm going to go on a trip, you don't have to worry about finding a charger. So it's on the spectrum of electric cars versus if you go to like a Rivian or a Tesla or something like that there is no gas engine. So I used to have a Nissan Leaf; it was the same thing—no gas engine, had about 100 miles range. So the range of the battery was further, but if I wanted to go on a road trip, I would have to, you know, stage charging, you know, stops along the way, right? And so it's on this sort of spectrum, but my observation is just, you know, I'm actually really excited because I think the diversity in the market—I think, you know, being able to find abundance of choices, not that there's anything particularly wrong with Tesla, but just having
B:more competitors in the market in general. I think is really awesome and it's exciting to see these electric cars start to come out and start to kind of like make it easier for people who have them to more likely to find a charger somewhere because people are more incentivized to build chargers. Still, sometimes I'll show up somewhere and like charge, and people kind of look at you like, oh, that thing gets used. I thought that was just like a reserve spot that, you know, has the charger added at the mall. Like some of the economies around that are a little weird still, but it's kind of exciting there. There's lots of debate about where the energy comes from in the ecological, the environmental impacts. There we go of making the electric cars versus making gas car, trying to get into all that, but just as like convenience and I don't have to go to gas stations, there's like less sophisticated moving parts in a fully electric car versus, you know, the kind of hybrid—it's got more because that's, you know, kind of a lot of the traditional stuff and the electric car stuff. But, you know, it's kind of nice that in general not having to go to the gas station. You just kind of forget how inconvenient it is, especially where we happen to live now for whatever reason they just haven't like looked at the census data who knows to like build a gas station. So there's like no gas station on any of our regular occurring routes, so we have to like go visit the gas station to get that, which
A:is horrible going to the gas station. So when you say you plug it in—it's a plug-in hybrid—did you have to get the 220-volt adapter for your house?
B:Yeah, so this is a good question. So we don't—so we just charge it off of regular thing. Like I know we could do it. We could pay—we price it out. We just chose to do regular because our battery is small enough that we can charge it up overnight. Basically, if you have a 300-mile range like fully electric vehicle, it would take you sort of—I don't know, I'd have to look probably like 30 hours or something, 20-something hours to charge, so if you drained it all the way down and there's no backup, right? So um that would definitely be a call for a quick charge versus for me, it's only if I like went somewhere far in the morning and came home having [it] 220 would only be useful to like get it back up so I could go again in the afternoon versus now if I do that, I just have to use gas.
A:Yeah. I mean, I think this is super exciting. Um if you go on a long trip with a seven-gallon tank, are you refilling very, very often? Or how does that work? So the
B:car basically reverts to being a normal hybrid. So it still gets—I don't know whatever the mile per gallon stuff is, a little weird anyway. It goes to whatever the normal mile per gallon you would get on a hybrid is. So it's still very efficient. It'll still shut off the engine; it'll still do regenerative braking like you were saying. So I think I get something like 40, 50, 60 miles per gallon. Oh, that's great! So it's still very efficient. So that's a nice fallback not pitching plug-in hybrids. They all have their shots. Just kind of like you heard it here first. No, no, and I think like I said, I think Honda stopped making my car, so they don't even make it anymore. Oh no! And where I live, they never sold the car in this state. So actually, it's like very unique. I've only ever seen one other of this car in the entire time I've been living here because we moved from California. So in California, they sold a lot of them here; they don't. So actually, I'm like the only one.
A:with that car? Yeah, I think in California you can't have a combustion engine car after some time. Like, yeah, pretty
B:Soon they're gonna change it. Yeah.
A:Yeah, that—it's really interesting. Yeah, I have a friend, another friend who has a fully electric car. I can't remember what it is. Not a Tesla, but it's something else, but it is fully electric. And you know, he was—he, I think it is a Honda. Does Honda make a fully electric as well, or no?
B:Is that the IONIQ or something? Maybe. Yes, it could be, but...
A:Oh no, that's—he would—he was saying that. You know, the nice thing is, you know, it's just as you said, you never have to think about the gas station, and plugging it in when you get home is so much more convenient than the gas station. Plus, you know, the gas station causes some like latent anxiety. It's like you watch it—it's like a half full, a fourth full, an eighth full, and then you go in. But that whole time, you know, you have to kind of keep it in your mind. So yeah, I think I have a feeling like the next car we buy once one of our cars finally kicks the bucket is gonna have some type of battery in it. Well, your...
B:Car has a battery in it now? To be fair, okay, I know.
A:What you're saying, some type of battery-powered?
B:Engine propulsion? Electric propulsion of some sort.
A:Yeah. I think the hybrid has a lot of nice advantages, so I have a feeling the hybrid is probably what about the gasoline like if the gasoline sits in the car because you're not using the engine? Is that bad? Do you have to every now and then just go on the highway?
B:Or something. Yeah, I mean, if you probably over the course of a very, very long time, but I do notice—I'm not a car nerd as anyone who is, probably already is, moaning and transporting, but the engine, the gas tank is like I think it's pressurized or something to help avoid that. So like when you push the gas tank release button, it actually takes like a good five seconds before it makes kind of a special noise and then opens up. So I think they do something to keep it like a more sealed thing to prevent which makes sense. Then yeah, you know, a lot of the issues you might otherwise have, sort of don't pop up, but I've never had an issue with it, and we do routinely run it, you know, every so often. So we do go fill it up every couple of months. It's not like we go two years and never, you know, never use it. So...
A:Very cool. All right. Yeah, interesting. We'll have to see. Well, actually, we'll end it with a prediction like at the end of this year how many what percent of cars will be either hybrid or electric? Actually, I don't know the percent, right?
B:Now. Yeah, this sounds—this sounds sketchy. I'm just gonna say I think it's like you—I think the prediction is like what will and there's no way to get a metric for it. It's just like what percentage of people will seriously consider, let's say sort of on the spectrum, a plug-in hybrid to electric, like something that can drive, you know, some tens of miles without using gas? Is that going to be, you know, a third of people? A half of people? I think some people, like you're going to buy a pickup truck because you work construction. You're not probably going to consider very strongly a fully electric car. Maybe that'd be great, but so in the next five years, what percentage of car purchasers will consider like a fully electric vehicle?
A:Wow, this is nuts. I don't know if this includes hybrids, but it says this is April 3rd of this year: Electric vehicles account for less than one percent of vehicles sold in the U.S. I feel like that maybe you know it's just perception bias, but man, it feels like that number is a low. All right. Anyways, on so yeah, I guess now that I've seen such a shockingly low number, it's hard for me to have a prediction, so we'll just table that. You want to just take one percent? Yeah. My prediction is two percent. Oh, that's.
B:pretty good. That's doubling, more than doubling. That's.
A:Actually, how we do math on programming throwdown? All right. Time for news. You have the first news go for.
B:It yeah, I do. So I don't actually have a great article for this, which is like some running thing that I was observing. I think it's kind of the same person or people we've talked about code golf in the past. And this interesting spiral—this person was posting a sort of ever declining number of bytes that they've managed to make the game of Snake. You know, where you like turn left, right, up, down, eat the little pixel, I guess it's supposed to be a fruit. Snake don't eat fruit, but you know, like the snake going around and eating and growing slightly longer until you, you know, crash into yourself. So someone has been trying to get that assembly version of that lower and lower and lower in number of bytes. They were at the one I have here linked in the show notes. They made it to 101 bytes, pushing for that, you know, getting below triple digit number of bytes to encode the Snake game, but they've been putting it in a QR code. And boy did this get people very divisive about this. So really yeah, so I am aware of a lot of security kind of concerns, but I'm not overly—I want to say paranoid that adds a negative stigma to it, but I am not overly cautious about maybe stuff I do. I probably choose convenience more than I ought to.
A:Say yeah, same.
B:Here. But people were saying, 'Oh my gosh, why would you try to tell people to like take a picture of an untrusted, unauthenticated QR code, especially one that you intend to run like who knows what exploit in it?' And then just like people coming out now. To be clear, I don't know what percentage of computer professionals would be represented by the noisy people in the threads, but basically like how it is a terrible idea to ever take a picture of a QR code because it could just be like an exploit waiting to happen, right? Like could take you to a payload. It could take you to whatever. Like you're asking, you're basically clicking a link, and everybody would say, 'Oh, don't click a link emailed to you.' But most people would say, 'Oh, you know, QR code. What's the worst that can happen?' Now I don't know how many vectors or how many proven sort of like problems have come through people randomly snapping QR codes out in the wild, but it is a concern. But I thought this is sort of one of those like unintended rat holes or whatever where like you think you're going to read about someone like, you know, clever ways or people collaborating on other, you know, optimizations they could do to get their size lower and lower, or just being happy, or 'Oh, how cool this is!' Or references to demo scene, you know, 4K demos, or whatever. And those things were there, but then this whole other like brigade of QR codes are the worst. It was yeah, I've.
A:Seen. I've seen some video where somebody basically showed how to exploit this where they were at a restaurant and they stuck a sticker on top of the QR code for the menu that then like sent them to their own site. But yeah, I mean, what you're hinting, what you're pointing at is interesting. It's like the serendipity of the internet where you know, I had—I mean, you don't have any social media accounts, Patrick, so I'll have to lecture you on.
B:I had.
A:A post on LinkedIn. Basically, this is years ago, but I was just really excited that I got promoted, and I was based on my post. The gist of my post was, you know, I never thought that I would really make it this far. It's really happy and thankful. And I think like, you know, some professors from like a long time ago and stuff, right? And it was—it felt really good to make that post, and that post went viral and got tens of thousands of likes on LinkedIn. And then basically there was a whole ton of comments about how my university is like a crap university. And I, you know, it was the thing where like I wasn't mentally equipped to like to moderate something like that, so I basically just left it. And it turns out LinkedIn is really good at moderation because I went back months later and all those comments were gone. I don't know how that happened; it wasn't me. I didn't delete it. Yeah. So there's something some kind of moderation going on there, but same kind of thing where it's like yeah, I made this post just to talk about, you know, just celebrate something, and it turned into like this whole back and forth about like where I went to college, and that's just the internet. I mean, it makes it like kind of scary to say anything.
B:Oh dear. Yeah, I don't know. Yeah, I'm probably doing the right thing YouTube videos and I see the comments, and I'm just like, 'Oh, I don't know. I can never be a YouTube creator.' I would just have all my comments turned off. Just yeah, about the littlest things. There's no like sense of collaboration or like—like you said, you know, people just move on. Like why didn't you take pot shots at someone's university? So yeah.
A:I mean, I think we had comments initially on the blog, and there's a lot of spam, and there was like some divisiveness, and so we just decided to turn it off. And yeah, comments—I mean, that's a whole separate rat hole, but like whether you should have comments or not, I mean someone should write like a dissertation on.
B:That. Yeah, there's some—I'm not much into the like publicize your podcast. I was listening to a podcast the other day, and the podcasters on the podcast were talking about another podcast where they were saying the person's thesis is sort of like you want to be really controversial because like this stuff, like you're saying, you should have actually leaned into it and like doubled down on it and like gotten people riled up because then people will make other posts that link to your post which will make your post even more important. And like basically you should be like controversially optimizing so even if you don't really believe it, you should like make a follow-up post saying where I won't say because you didn't say it the name of your university is like the best university, because now you know like that yeah, right? You have been given a pearl. You have been given a surprise treasure which you now know that your university is apparently divisive. Therefore, you should double down on it and start more controversy, and this will like propel you to fame and theoretically fortune or success or whatever these people are optimizing for. Yeah, they.
A:Call this the Attention Economy, right? I mean, I was listening to something about—I listened to this board game creators podcast because I think people who make board games for a living are just fascinating. You know, and they were talking about how to do well on Kickstarter, and basically a big part of it is already having an audience. So so I mean, you know, one sort of really odd way to get there is to start a bunch of controversy, build up a lot of followers, and then bring them all over to Kickstarter to buy your stuff. Oh we.
B:Should okay. I don't want to get under the Kickstarter. Yeah, that could take.
A:Forever. All right. Yeah, all right. My my my news is superconductor rumors abound—so room temperature superconductors. So um, I'm sure you've heard about this Patrick. Uh there's this thing, I think it's LK-99 or 77 or something. Okay, 99. It's it's uh some kind of and you know when I read the instructions of how to make it, it didn't look that weird. I was kind of expecting the instructions to be like, you know, like build a nuclear bunker, you know hit chemicals with nuclear bombs or like like that's like something crazy like you'd see from CERN or something, but no. It's actually like here take these materials, do this stuff, and it seemed like yeah, like relatively straightforward. Um, and so uh they're claiming that you'd have a room temperature superconductor, which you know I think if true, like opens up a ton of different possibilities. I mean, just one, and I'm sure Patrick you have even better ones, but just off the top of my head, you know think about how much power is wasted in getting electricity from the power plant to your house, right? I don't know the number, but it's got to be a very high amount of power is wasted. And you know if you had room temperature superconductors, you could theoretically bring that to zero or at least get it a lot closer to zero. Um, and that's just one of, you know people are saying you could have GPUs that don't get hot and stuff that I'm not totally sure how that would work, but uh but there's definitely huge application for it. And uh now everyone's rushing to see oh, and then there was all this controversy around I guess the way they announced it. They didn't go through the typical channels. And and so you know people are are not sure whether it's real or not. And uh uh it's just kind of a crazy situation right now.
B:Oh man, like this is similar to what we were just talking about opening sort of Pandora's Box, which of the random threads should we pretend to be experts on? The amount of like people coming out of the woodwork claiming to know things about superconductors or like what their implications are or like whatever on on sort of I guess we call it X now, about like right on X—Twitter—the social media network everyone's on.
A:X. We have a serious.
B:Epidemic. Oh no. But yeah, so so I mean like a couple of things just to like like my my sort of like thoughts to your thoughts. We have heard a lot of the same stuff—a lot of weirdness around like how this was announced, a lot of debate that I've seen which is kind of interesting around like should we go through the normal peer review process or is this actually like better? This distributed stuff and people sort of countering, well, this is a big waste because if this truly is a fake or like a dead end, then like all of this like random people stopping what they're doing, startups putting money into this, and like this race to all become the first to market or whatever it is, like a waste of resources on a global level. It's much better to have like one or two very focused people sort of proving it out. Other folks saying, well, no that's the way you just end up with patents and then it like is protected. Very interesting discourse. I'm not an academic person so I have no I have no pony in that race. So moving on to the next one, but like you know your sort of claims like oh imagine like wasted power lines. Well, so there's a whole thing of like economics. Like even if you took it to zero, the thing still has to be as cheap as copper wire and even then it has to be a good point. So even like even if it was a room temperature or even high, you know above far above room temperature, it might not be very ductile. It may not be able to be put into wires right easily or economically. And then things like you're saying people were talking about oh batteries that never run out and your phone and like GPUs that don't get hot. And then this descends into which I was telling people Landauer's principle and reversible computing, which is this idea like if you destroy—we have referenced this before—but if you like make a computation, like an AND gate is one and zero, zero and zero, zero and one all have the same output of zero. So information is lost. You had two bits of information, now you have one. And there is a theorem sort of saying that losing that bit of information has a minimal threshold of like wasted energy because of.
B:the lost bit of information, which says even if you had superconductors, even if you had new silicon whatever GPUs would still get hot. Maybe not as hot, maybe you don't need a fan, but they're not going to be it's not like a zero end game. There is still waste energy. Your iPhone, your Android phone and your pocket still will consume energy. And so there's like a lot of this like you're sort of saying oh we'll just you know put put electrons into the superconductor in a big loop and then we'll have you know basically better batteries. So folks are pointing out actually superconductors have like this quenching property, which is if you if the magnetic flux again not this is not my background, basically exceed some amount—basically put too much electron circling around they build a magnetic field. The magnetic magnetic field actually sort of quenches the superconducting ability. The thing that makes the superconducting above a certain magnetic threshold like causes it to fail. And this is a big concern actually in MRI machines. The Large Hadron Collider had a quench event that apparently like the magnetic flux and one of the big magnet coils that are superconducting that they have that they chill to very low temperatures today had like a over too high of magnetism, and it basically destroyed the superconductor and caused like a large portion of the Hadron Collider to need to be repaired. So there's like all these like second-order phenomena. So people just hear something akin to like perpetual motion or free energy and get like whoa, it's like well hang on. It is a huge market. It is it is going to open up applications we don't know of today, but it's not like every bit of history is like rewritten because of you know a magic material, but still very interesting. I watch it every day. I log on to see like has someone been able to there's a lot of weirdness and how it gets made and no one really knows how to do it. And uh so is it true? Is it like meaningful? And you know can we actually figure out a way to produce it? It may be a decade before like even the first practical.
B:applications come out, even if it turns out to be the sort of like holy grail everyone was seeking. Yeah, here's.
A:What I think we need to do Patrick. I think you and I need to start a petition saying that for the next six months we need to have a moratorium on superconductors and only we can work on it for the next six months, and then we need to I just walked right into.
B:That one. I didn't know where.
A:You were going. We need we do superconductor safety is like a primary government.
B:Should license.
A:That's right. Yeah, and everyone except us should be banned from doing work on superconductors. Are you gonna?
B:At least tell people what the joke is there.
A:Yes, that's a reference to the six-month moratorium on AI that was proposed. I think even Elon Musk signed it. A whole bunch of people who would definitely not stop for six months signed it, but they want you to stop for the next six months. All right, on that note, Patrick, you want to talk about OpenWorm? What is
B:Okay, yeah, now on to something completely different, but I also don't know about, but I found this. This is fascinating. So I've seen this for a few years. It came up again recently. They made some progress. So OpenWorm is a project to attempt to create—I think it's—I don't even know what the 'C' stands for. C Elegans, some kind of little flat worm that is compute composed of a few thousand cells, and they want to make a simulation of the worm. Now many people have made like simulators of worms before, but they normally kind of go top-down. So they sort of start with the behaviors and then they make like a simulator or model that like does those behaviors. Right? I mean, we all play video games. It's basically how video games work. Like when you have a video game, you know, enemy that you're chasing around a level. They are not simulating the cells of the organism; they're like taking the behavior and then like applying behavior to a sprite. Right? It's kind of more top-down, but here they're actually trying to go bottom-up. So because this Elegans—I don't even know if I'm saying that right—worm is so simple, they have done what they call like the connectome, so the neurons that you know are the worm's intelligence, they know how each of them are connected to each other. And so here they're trying to model the actual like chemicals flowing between the neurons, the electrical impulses back and then saying things like, 'I move this cell is a muscle cell, so if it gets this kind of level of chemical, it's going to shrink in size or grow in size,' and that's going to move against the environment, and then that's going to cause pressure on this sensory nerve which is going to uptick its chemical output, and sort of doing this entirely bottom-up, sort of not at the like atomic level—like there is still you know some amount, right? Picking something in the middle, but there's much more sort of cellular level model each cell and then sort of see if you have the emergent property again of like basically a silicon simulation of the worm that could one day even, you know, find mates.
B:And have eggs, and those eggs could grow up in this environment and even, you know, amazing refine their genes and pass on stuff. And you could just—but right now computationally, this is like even for as far as we have for a simple, simple worm, like that's actually I don't say cutting edge to kind of do this bottom-up for a few thousand cells, and you can sort of go watch the videos not in real time, but of the sort of worm wiggling around its environment and trying to make its way. And it's just like a really fascinating like something. At first, you'd be like, 'This is really silly,' and then you're like, 'Whoa, I don't even—like it would take me probably a month of or more reading to actually even appreciate what they're actually doing here.'
A:Yeah, this is wild. I'm trying to figure out who actually is behind this.
B:Oh, like what group?
A:Yeah, well it's actually like they have a board of directors. Wow, it's like a ton of folks. It must be some kind of—it's a not-for-profit corporation. This is wild. Yeah, folks should definitely check out this video. At first I thought when I saw OpenWorm, I thought it was gonna be like a worm virus, you know, like computer.
B:Virus? I thought the same thing. I was like someone put source code up for like a computer virus. Yeah.
A:Exactly. This is phenomenal though. It really looks like a worm the way it moves because they're modeling it as such a law. So it did how do they reverse engineer the brain? I guess. I mean that.
B:So I think this is like it's a well-studied organism. So for like one of the muscle cells people have isolated it and like observed what stimulus makes it do what output, right? And so they have models for most of the cells, and like I mentioned people talk about like the genome or like brain scan, but this connectome, which is like neurons are very these isodendrites and the whole thing, right? These neurons are very long and spindly. This is not how neural networks work in the sort of like large language models, but in sort of biology there's like these very long tendrils and those tendrils overlap and touch each other. So if you sort of freeze very cold like the sample and then you slice it very thinly and then you apply like computer vision to it, you can figure out this cell touches that cell there so they can pass chemicals between an emitter and a receptor, and so you can figure out the sort of network of all the cells together, and then they're basically attempting to simulate that. And this is a relatively simple organism, and so it's—it's sort of attainable. Wow.
A:That is wild. Very cool. Folks, you definitely go to the show notes, check out the video. This is pretty awesome. All right, my news this is a relatively short one but sad one. The creator of Vim passed away. So he was in his 60s, so he wasn't that old—62. Bram I'm probably gonna get this wrong, Mulinar? Um and I don't—I'm assuming he had some kind of health issue. I don't know if it really said oh yeah here we go a medical condition that rapidly progressed. It didn't say exactly what it was, but you can actually see on GitHub, you know, he has all this activity, and then you know right around—I think September or October, or September or October—the activity just stopped. At that point, he must have got some kind of diagnosis or something. So, you know, yeah, I mean I actually wouldn't say I've never used Vim. You know, it's been on the computer sometime and I've used it because I didn't have Emacs. I've been more of an Emacs person, but I know Vim's been just incredibly influential on everything. Did you ever—were you a Vim user, Patrick? I mean only?
B:By like I don't want to say necessity. I've never hardcore developed and I have edited files in Vim, and I know how to sort of get around, but I've never customized it or attempted to use it as
A:My IDE? Yeah, same here. But um oh, it looks like he's from Holland originally, but yeah, so you know best wishes to his family and you know someone who really created something really special for the community. And time for Book of the Show. Patrick, what's your book of the show? My book?
B:of the show is the little book of common sense investing uh definitely a good book uh pretty like i would say easy reading but more importantly sort of like what it represents i guess anyways this is a book by uh a man named jack bogle who passed away a few years ago but jack bogle uh probably most famous for uh starting the vanguard group and so vanguard group which a little united states focused for a minute but also i mean has applications to i think a lot of financial markets but basically um his philosophy was rather than doing sort of trading or picking individual stocks he sort of espoused the idea of index funds and of diversification and buy and hold so rather than sort of there are lots of other philosophies you can talk about in a minute anyways uh he sort of had this like just keep it simple you know have you know what you think your you know allocation should be and sort of just like invest in it and this will save you fees it will prevent you from getting into market timing which he thought wasn't uh you know a thing worth doing so yeah like a lot of uh core principles i feel like now they've gained a lot more traction than even sort of 15 20 years ago um this was a little bit more uh outlandish people didn't have 401ks auto invested in target date funds he was really part of the group that like helped make that uh a sort of like uh thing and so retirement funds being just in some some allocation of equities and stocks uh he has there's a group of people who are very diehard into this sort of like way of doing they call themselves bogel heads after after anyways uh so uh a deep rabbit hole for sort of investing but the little book of common sense investing comes with a lot of other sort of like ways of thinking about money i would say uh in addition to just sort of like the investing and savings but just the importance of that um there is a a bunch of rabbit trails to run down here without turning it into a finance podcast about uh just
B:interest but i will say i was uh i saw some statistic the other day just talking about how much growth there has been in sort of broad index investing across the world but but in the united states we have something like the s&p 500 or the russell 2000 the biggest 500 by market cap companies and the amount of people investing just in all those 500 rather than not and they call this sort of passive where you just buy you don't care if it goes up or down you'll sell when you retire or you need the money but you're not trading the amount of passively invested money there's a lot of uh interesting debate about has this changed market dynamics is there like an end game where markets are somehow like messed up because it doesn't matter what your company reports as its earnings like everybody's gonna still buy it because it's part of the s&p 500 there's a lot of debate about uh what sort of changing i won't get into to sort of my my thoughts there the efficient market hypothesis there's a lot of a lot of stuff around this but anyways a lot of folks uh listening you know early in your career or or not and sort of wondering of a way of thinking about money you could do a lot worse than picking up this book and sort of thinking about just diversified holdings not trading just being really sort of straightforward not making it over complicated uh and this is this is sort of like the vast way of uh i think modeling how i think as well to in part because of him and others uh in a similar vein yeah
A:that makes sense i mean just a short story if you if you don't diversify if you take huge risk you could potentially get huge reward but you could also get completely wiped out um you know i know um if you are going into industry and they're paying you in stock then you're potentially going to end up with a lot of shares of stock of your company that you already work at and um you know i've heard stories where people said hey you know i've worked at the same company you know i was one of the first employees i've been there for 10 years the company's doing amazing and i i never sold any of my shares and it just exploded and that's because they took this huge risk and they got this huge reward and that's great you don't necessarily hear the stories of the people who you know worked at companies company you know rose up the company went public so that they could sell their shares they held on to everything and then you know inevitably in the next five years 10 years 15 years at the next point of time at some point the company collapses um and and that person loses everything that does happen uh more often than you know as a community we want to admit and those stories aren't amplified because no one wants to to do that so so you really have to be careful and uh yeah i'm with patrick on this i think um you know reading some material when you when you're young can help you exponentially in the future
B:the compounding growth stuff i mean it it really like now not that i'm old person but i feel old uh not that but like you looking back and sort of you know it doesn't feel like much you know a little bit here a little bit there but then when you keep doing it year after year and i think it's actually a philosophy i feel mirrored and how i think about my career growth as well is just i'm never looking for like so i'd have this oh we see this now with large language models coming out people jumping on like oh i'm going to be a prompt engineer i'm going to figure out you know how to use you know this or we're talking about okay 99 i'm going to go like figure out how to synthesize your super okay but like that is a way i'm not saying you can't it it has a a more dispersion in sort of like your outcomes you might do really real well often you probably won't the average case probably not that great but i feel like in your career just looking for incremental each year what's you know one two three things you can do or learn that add on to what you already have i feel like this is a way over the course of a career 10 20 30 years all right you can be really really good at stuff by the end if you're just making sort of compounding incremental progress but maybe that's a a broader totally agree totally
A:agree also uh just just to riff on that a little bit and then we'll move on but um a lot of the times these like rags to riches stories are kind of bs and and and when you see something small turn into something really big there actually was a really big concerted effort to make that small thing big and so you know actually a good way peter teal puts a pretty good way he basically says if you're a big company you want to not seem like a monopoly because you don't want to get broken up and if you're a small company you want to seem really big um because you want the investors to get really excited and so it creates this really weird parabolic effect um and so a lot of a lot of uh um a lot of of of small companies actually maybe had a lot of of push from the outside and they weren't actually that small and so i give you the impression that like yeah in your basement you could start next billion dollar company but but when you actually dig into the details a lot of the time there's like huge institutional backing even from day one um all right we'll move to my so patrick i did this for you um yeah i
B:see this i'm excited let's go
A:yeah so i decided to read more fiction my son basically the way this happened um my son finished harry potter the second time he he read it through it twice and uh one of my co-workers suggested brandon sanderson because he has a who's that uh i think it's called skyward the one for kids yes and um so i did that and i thought you know i really should read more fiction i read all these programming books i read some economics books philosophy books um you know and i just need something lighter especially i knew i was going to be on an airplane a bunch the past couple of months and so i got into the mistborn saga and it is awesome i am a big fan um i remember you talking about this years ago and not wanting to spoil it but but uh just to recap what patrick said many years ago there's people who can manipulate metal um in a way that's really magical and um there's all sorts of uh things that that that are spun off from that one idea and i i think um um i i've only finished i'm about halfway through the second book so i still don't okay quite understand why and i'm not spoiling anything here you know why there's ash all over the place like what actually caused the earth to be destroyed like this like i haven't uncovered any of these secrets yet but i'm a book of an and a half in i'm i'm really uh excited and uh it's um it's something that uh i've been really enjoying so so it feels good to get back into fiction i think i'm going to
B:keep it up yep they and now like you know this was the original trilogy but then there's also now like additional books that are set after the that trilogy so you actually have a lot of runway ahead of you uh and then now that you're in the uh brian's that brandon sanderson like thing you'll find out that the system he's applied here is actually part of a meta system so like the way that metals sort of work here is similar to how other things let's just say work in other locations in the universe of his books and so the there's a treatment of metal in sort of this that is handled by other things in other series and so and there are sort of like intertwinings between the series as well they're not completely isolated so um yeah just to kind of like set the stage like to dip your toe and then not to get scared but like yeah you can keep spiraling further and further out from uh from where you are but yeah i mean those first ones it's someone there's nostalgia like you almost miss miss going back and like that first kind of like oh what this is so crazy and it's so cool yeah i remember it's those are good they tried to make it into a video game i think but i believe really out i yeah i think it's stalled out yeah
A:oh yeah i thought um um i thought that was a clever way of saying it basically like imagine if this whole section of the periodic table was like off limits it's like kind of like a weird thing to think about but it's like a good premise behind building a universe you know it's really because it's you know one thing that that i try to shy away from are things that are like futuristic like star wars and it's not that i do i have any apathy to that or or not apathy but any it is really apathy like when it starts getting into laser beams and spaceships and stuff i just it just not it doesn't really kind of engage me the same way but here it was you know it was real enough for me that i could kind of see it in my in my mind's eye and uh um i might end up getting into the into the i know that that there's skyward and all these space ones i might end up getting into them
B:Mostly, well, I'm excited. You have to keep us up.
A:To date? Yeah, totally. And I've been jumping into Tool of the Show. I've been reading this on my Remarkable, which I've been really happy with. Do you have one of these, Patrick? I do.
B:Not. I do not. I do.
A:No. So I've been wanting—I had a composition book full of notes, and I'm constantly flipping among different notes. I had basically a page in this book for each person on my team. I had pages for different topics, and so you know if someone would say something that I would need to tell someone else, I would flip to that page in the book, and it just felt kind of silly in today's day and age to be doing that. But the reason I was using a composition book was that the act of writing really helped me remember. I not only remember the content but remember that there is something on that page that I need to take care of. And so it was, and also I could go back through and cross things out, which I know you could do that at Google Doc, but I just found the writing part really, really useful. So, I thought I would get one of these kind of note-taking tablets. There's a bunch of options—there's a Boox, there's Amazon has a Scribe, which looks pretty good. I settled on the Remarkable because it was recommended by a couple of co-workers, and I think it's great. You can read books on it. You could take notes with it. It has some other features like OCR and stuff, which I haven't really found them that useful. But you know, the core functionality is extremely nice. And I'm usually not one of these people who has any type of fashion sense or any modern gadgets or anything like that, but I will say when I pulled out the Remarkable on the plane, the people who sat on either side were very impressed. It does it is kind of like a very large Kindle, but it's extremely well manufactured. It's very thin. The contrast is extremely nice, and yeah, I would highly recommend it.
B:So E-Ink, right? Like it's—it's like you can oh yeah, we
A:should talk about that. So so it's E-Ink, which means I think the way it works is some electricity is forced in a certain frequency and that tells the ink to reveal itself or not. And that happens per pixel, and so that means is like it takes a little while. So for example, you can scroll, but when you scroll, it's scrolling at like half a frame a second or something, and so it's kind of a weird feeling. It makes much more sense like flip page to page. But then the nice thing about it is um the battery lasts for basically months, and while you're not doing anything, it's not using any energy. In fact, if you leave it for 30 minutes, it will turn off the Wi-Fi, turn off the processor, basically totally go to sleep and not use any energy, but you still have the whole screen like whatever you were looking at 30 minutes ago is still there. It just put a little icon next to it saying it's asleep. So yeah, I mean, I rarely have to think about charging it. And yeah, I read whole books on it and used like five percent of the battery. So so yeah, it's a good product. I will say the Amazon Scribe has backlighting; this one doesn't. And so you know for an airplane that's fine because you have the overhead light, but you can't use this at night without—without you know having a light next to.
B:You now just imagine if you had a Remarkable and Kindle. Just imagine the battery.
A:It's like I start using it at 50 and then I have to turn it off because
B:it overheats from your hand. Yeah, all right, my Tool of the Show is Stellarium. I think that's how you say it, but just more broadly there's a class of—I'll say sort of astronomy apps. I've always had this thing like I was never big into astronomy. I don't know if you ever were, Jason, but I had a telescope once, but I was never very good at it. So I started to get interested in it again, and one of those things I think I downloaded when I first got a smartphone where you like point your phone up in the night sky, and it tells you, you know what? Oh yeah, I still use that. Yeah, not by image recognition but just by like compass and accelerometers and understanding pose. But they just—I don't know, like I guess I just continued to work on it, and I feel like I used it and it's just like oh this is actually really cool. And then also being able to know like when is something I want to see? I want to show my kids Jupiter or whatever, right? Like, oh well, when is Jupiter going to be in the sky? And just like having it's just very cool. And I was—I'm trying to gonna try it. It's been really cloudy here, so I got this idea that I'm like I'm gonna do this. I had an old telescope. I got out and like and it's been cloudy every night, so it hasn't worked yet, but I haven't been taking this still area out and like trying to look up and like imagine what the stars would be. And I you know it's just pretty exciting. One of those things I kind of forgot existed even though—I mean, I guess probably lots of people use it. I think most of these are free. So there are others are free to download. They I think they charge you for like additional entries in the star database or you know if you want to search for certain things or do predictions about—I guess they're not really predictions, understand when something is going to occur. But in general, you can normally use them and poke around them for free, which is pretty cool. So if you've never tried that before and you have a smartphone, which probably do if you're listening to this, then I would recommend checking it out.
A:Yeah, I mean, I had my first moment where I felt like my son, who's 10, is like just light years ahead of me in something. And he—he's really into science, all kinds of science: biology, astronomy. He's constantly reading books on it. And I was pointed to this star that was really bright. He goes, 'Oh yeah,' he's looking around the sky. He's like, 'I'm pretty sure it's Venus.' And I thought, you know, 10-year-olds like they're just making stuff up all the time, right? So I pulled out—I don't know if I'm using the same app as you—but I pulled out one of these apps, and it was Venus. And like it totally blew my mind. But yeah, that stuff is super, super fun. It's a great time if you're ever watching fireworks or ever in a situation where you're out at night. It's really fun to pull that out and see all the constellations and everything. Yeah.
B:When it has all the constellations on there, I'm just like, 'Oh yeah,' it turns out I don't know anything about any of this. If you had pointed out, I'd be like, yes star, and I would have been wrong.
A:Oh man. All right, on to the topic of recursion, the scariest topic for every first or second-year CS student. Now, I mean not for me either, but I think that in aggregate people got more scared about recursion than anything else. And I think you know we're gonna jump around a little bit in the notes, but I think the reason why it was scary is because the way that it was taught at least at my university, which as we know from LinkedIn is a crap university—I actually love my university. I went to UCF, University Central Florida. I loved it. There it's a great school for CS. But they're kind of being the butt of today's episode. So you know people were taught sorting, right? And so sorting it's kind of like this tool like you immediately see how it's useful. Like you could see I have a list of things and I have a sorted list of things. It makes sense. And then they're taught structures like—you know a linked list, and what's another entry-level structure? Maybe a binary tree. Yeah. And so it's like okay, these are structures and I don't really know how they're useful yet. But you know, it's like I could see the structure and the geometry of it and understand it, right? And then recursion—it's kind of like they're trying to fit that mold to teach recursion, and it never really works. So what will usually happen is they'll say, well, you know, they—the one I think they use is making change. So they're like okay, in the American system, you can greedily make change where you just give people as many quarters—like let's say you need 74 cents in change. I just give you quarters until it's less than 25, and then I give you dimes until it's less than 10, and we just keep going, right? But what if there's this crazy coin system where like there's a seven-cent
A:coin and a three cent coin and a one cent. If I give you the seven cent, I can't give you the three cent. And yeah, I mean, I guess Patrick's like head is exploding right now. Well, no.
B:They taught you the Knapsack problem as like an introduction to recursion? That's very brutal. Yeah, and
A:It's like, you know, here's how you make change with, you know, and here's—here's why you can't do it the normal way because you're not in normal currency. And the whole thing was just like didn't make any sense, right? And it didn't really relate to other stuff. I feel like um, I think the reason they do this is if they were to connect it to other things and you didn't grasp those other things, now you're just kind of falling even further behind, right? So if they said, 'Well, here's recursion and how it applies to sorting,' but like the kids didn't understand sorting that well, it's like then you know they kind of totally lose them. But I guess in general maybe to step back a bit, recursion just wasn't in general, I feel like isn't taught very well, and that's what makes it so scary to.
B:So many people, I would agree. I wasn't taught that, like, to be clear. The problem you're introducing is this is pretty difficult. So we were taught it with, I think you—you probably encountered as well, but how to calculate the Fibonacci sequence, which for me was demotivating in a different way, which is it was such an obviously horrible way of computing the Fibonacci sequence like it is not, uh, like that is like not the way. As someone who, I guess started in like a more optimization C, like low-level coding way, as soon as you tell me that you're going to like—I understand what you're talking about with recursion, and then I'm like, 'No, you do not want to do this. This is not very efficient.' And so it bothered me for a different way. I think though, you're right. I think this is not a—it is a natural phenomenon, but not in a sort of like intuitive way you would encounter perhaps before you would be encountering this. So if you've not encountered fractals or self-similarity before, this idea that you have a function and inside that function is an invocation of the function itself—this like self-similarity, self-recursive—isn't something you would be have been introduced to in math, although it does exist in math. You would probably not have seen it in the math you would have taken at that time unless you moved to Computer Science very late in your college career. And so I think approaching this like, 'Hey, and we're going to talk about in a minute,' but like, 'Hey, you're going to just infinitely go down and down and down and down or forking all the way down.' You just start to get this mental model that's very difficult because it's not this sort of iterative, uh, you know, imperative way of programming anymore. In fact, right? Like functional programming itself bears a lot of semblance to this versus imperative programming. So you think you get a lot of those ties and then also the failure case when you mess up your sort of end condition is your program crashes. And it doesn't crash for like a good error; it crashes like out of memory, which for, you know, if you're in
B:Java and not sort of C or C++, where you've already encountered a thousand memory leaks. You're probably like, 'Oh my gosh, like what is going on? Like what does this mean?' Right? Like you're
A:or like a 10,000 pages of a stack trace.
B:Something. Yeah, exactly. Like you're not getting useful debugging information when your program crashes and when you try to print something like Jason is saying, you just immediately get like, you know, pages and pages of logs until your stack overflows and then you crash. So the like interaction with recursion at least in most of the languages that I'm familiar with being taught in college—uh, it is or high school—it's a bad experience.
A:Yeah, that makes a ton of sense. You know, it's um, um, it's a really, really, really good point that there isn't a print function which like keeps track of the stack. There isn't a print function that's like, 'Okay, here's this variable and here's the same variable, you know, in the parent function and the parent of the parent all the way to the root call of this function.' You can't see that all in one line unless you—you know, build that into your program, which is not trivial. Um, and so you're right. What you're looking at is, you know, I see all of these prints, and they're all from different invocations. So imagine like the Fibonacci sequence, and maybe I'll take a little bit of time to cover what that is. So I think it's F(x) minus two plus F(x) minus one, something like that. Yeah, okay. So Fibonacci sequence—um, so there's it's it's a function of x. X has to be an integer, and so it's a discrete function. And F(0) is 1, F(1) is 1. So those are your base cases, right? But then F(2) is going to be F(0) plus F(1). So in this case, it's going to be two. F(3) is F(1) plus F(2), right? F(1) is 1 and F(2) we just calculated as two. So F(3) is three. But then you can kind of imagine how this is going to start growing really quickly because, you know, you're adding these numbers that they themselves are getting larger and larger, and so it grows in probably some kind of quadratic way or exponential way. Um, and
A:So if you want for example F(100), then that is F(99) plus F(98). Now if you've already computed those in the past, then that's fine. But if not, you know, you have to compute those, which means now you have to call two more functions for each of them, and you can see it turns into this—this binary tree, and all the leaves of this tree are going to be either F(0) or F(1). But as you kind of climb up this tree, you're getting larger F values, and you're getting a lot of repetition. You could potentially be calculating F of four, you know, thousands and thousands of times to get to F of 100. Um, and uh, you know, and once you finish, you're—you know, computing this whole tree, then you get your answer. But you know, depending on how you write the recursion, you're kind of going through this tree in kind of different orders that might not be very natural. And so if you think you have some error in your Fibonacci function and you put a print that print's going to give you all kinds of different numbers from all kinds of different contexts, and it's very hard to debug. I mean,
B:I'm not a teacher and I'm not an academic, so I've not attempted to teach a version Fibonacci sequence Knapsack problem. I don't know. I think maybe we talked about sorting. I think actually attempting to teach something like a Binary Search or a Merge Sort, which is not how I first encountered it, but where you have actually like a fixed-length list bounds the problem a bit, right? Where if you screw up your indexes, a simple print statement would sort of tell you you're either repeating an index or you have like your lower bound is flipped with your upper. Like some very simple detective work would sort of reveal to you that you've encountered this problem. And I think the exploration of a tree or of all possibilities, which is sort of the Knapsack problem and the Fibonacci sequence, feels like mentally more of a large step than this sort of divide and conquer, right? Like I'm splitting something into smaller pieces. And even though I guess it's kind of the same thing, it's a quantized very quantized thing, right? Like I have a list of eight integers, and then I have two lists of four, and then four lists of two. I'm gonna mess it up. I keep doing this anyways. And so I feel like this is a more—I don't know, it's intuitive maybe for some. It's not, but I feel like that is the other like piece of teaching the recursive is using it for sorting and Binary Search, and we're not even getting into trees or this just yet, but I feel like that setup maybe—maybe sort of the sorting itself, like why a sorted list emerges from Merge Sort, is a bit harder to grasp, maybe. But the sort of like operational bit of it makes a lot of sense. Yeah, I think.
A:You're right. I think you know the reason why they use Fibonacci and Knapsack problem is because, you know, if you're doing like a tree search, then you don't need any memoization, right? Because you look at something once, and if you got your answer, you're done. And if you didn't, then there's nothing to really remember because you're never coming back. And so they—they try to teach, you know, recursion, memoization, and the ultimately like the sort of algorithmic way of doing recursion all at the same time. So I'll explain what that means. So, Fibonacci numbers, right? We just talked about if you have F(3), it's F(1) plus F(2). But F(2) is F(0) plus F(1). So you see F(1) is there twice. You need it for F(2), but then you also need it for F(3). And so, you know, if F(1) was really expensive to compute, then you're paying that cost twice, right? So you could do something called memoization. You can say, 'Well, I'm going to start with F(100),' and when I compute F(98), I'm going to store that somewhere, just in global memory somewhere on the computer. It's like, 'I computed F(98); it's whatever it is—a zillion whatever.' Um, and I'm going to store that in a table somewhere. Then when I go to compute F(99) and it needs F(98), I just go to my table and fetch it. I don't have to compute F(98) more than once because I know there's no side effects every time I compute this function, every time an 98 comes in, exactly the same number is going to come out. So if I could just store this, then—uh, then I would only have to for F(100), I'd only have to compute 100 things because
A:even if I'm calling F of 98 twice, I'm only computing it once. And so that gives you a really nice bound on how much work you're going to need to do. And in the case of Fibonacci sequence, you know, F(100) could actually take a while on a computer doing it the naive way, but as soon as you do this memoization, it goes from taking seconds or even minutes to like taking, you know, I don't know, 80 nanoseconds or something like an insanely short amount of time. Um Then you can say to yourself, okay. Well, you know I still have to deal with like the whole recursion thing and the call stack and calling and what happens if I even just doing if I do f of let's say 100,000? I need my recursion limit to go at least 100,000 levels deep even with the memoization, right? And so maybe my computer can't handle that. So what if I was to instead do something a little different? What if I was to say, okay, f of zero is zero, f of—or sorry, one, f of one is one. And and I already have those; those are in my table. Let's say I put those in first. So now what is f of two? Well, I don't have to do any recursion because I know that anything less than two has already been computed. So I just say, well, f of two is f of zero plus f of one and not literally calling f in that time, but just pulling those numbers out of my global array and sticking the two answer in, right? Same thing when I go to compute three; I already know that zero, one, and two have already been computed. And so I could just write the answer for three and write the answer for four. And so you don't even need recursion in a—you have recursion in a mathematical sense, but you don't need it in the computer. You just have a for loop from one to 100,000 and just write 100,000 answers. That's even better, right? And that's the I'm using the word agglomerative, which is just a fancy word for saying, you know, bottom up, but that's the way to do it where you don't even need to have the call stack.
B:You meant something different? I didn't. I'm not familiar with that word, but the adjacent thing I think to what you're saying in the same realm of like stuff taught here is the—is the sort of duality, not just with the math, but also is saying recursion. What is recursion at like a computer level? That's my background more than maybe the sort of analytical side at a computer level what you're saying. Is store where you were jump? I mean, all functions are this way—sort of store where you are on the stack and put information about how you want to invoke the function. So for Fibonacci, you're saying, 'Hey, I want this other Fibonacci,' and you give it a number, an index, or whatever, right? And you're saying calculate that. So I need to communicate to the new function of where I'm going jumping to—that information, but I need to store all of my state. So when you do this in your computer program, all local variables basically need to get put onto the stack so that you can go do this again, right? And that's what Jason is mentioning. If you do 100,000, you get this 100,000 deep stack. Well, that's because of all the extra overhead. The chances of you blowing up your computer resources are a lot higher when you do it that way. But you could also say, 'Hey, there's no difference between using the sort of the data structure stack instead of Q.' And so what do I need to know? I just need to know that I want to at in a loop; I want to, you know, either compute where I am or put new things on the stack and then pop things off the stack, right? And so the stack here has a duality. There is an actual data structure stack in your like CPU and RAM that your operating system is maintaining, but we're also saying like the stack is this sort of like concept of, you know, descending down in your recursion, and you end up with this kind of like fluid set of words that kind of describe all of them at the same time.
B:Interchangeably. But sometimes you can get around the limit of, you know, an operating system depth of stack by just maintaining a stack yourself and pushing items onto it and popping them off the back. And the evaluation order would be identical to your recursive definition in your program, but you removed recursion. Well, have you or haven't you? I mean, I guess it's a terminology thing, but this is the other sort of way of thinking about it. And so all of them are roughly equivalent; like all of them are the same. You are doing the same operation, but it's just sort of operationally how you end up achieving those results.
A:Yep, that makes sense. And so yeah, this whole thing where you kind of like reverse the problem—like the Fibonacci example where you want f of 100 but you just start computing from f of zero and you stop when you hit 100. That way of sort of reversing it, you know, it works for Fibonacci, and it works for the Knapsack Problem, but like the vast majority of problems, it doesn't work that way. Like, you can't say I'm going, 'Yeah, I'm going to search for a number in a binary tree,' but I'm going to reverse the problem. We're going to check all the leaves first.' Like, it doesn't—it doesn't make sense. But because they want to teach that reversing part, they can't use trees and binary search and these things as examples. And to your point, that forces them to pick these really esoteric examples. Um, and I can tell you from personal experience like I've done a lot of recursion in different contexts, mostly when dealing with graphs and stuff like that. I have never ever done the thing where you reverse it. Like, yeah, where you reverse it and you say like 'I'm gonna go from the base case and build this breadth first.' In the base case, I've never ever had to do that. Um, you know, hash tables are so so fast now um that you can do the recursion with memoization. You have never needed to go hundreds of thousands of levels deep in the stack. So a lot of these are kind of like not real problems. And and I think to your point if they had done—if if they teach recursion without that last step, they could open up the aperture a bit. Um, and yeah, kind of with that in mind, we can talk about, you know, what are the times, you know, practically like in our careers where we've used recursion? You know, I talked about in my case, the biggest one is graphs.
A:There's many times that I've had to deal with graphs. Um, I worked at this company where we had this big social graph that was—that was kind of important, but even more important were all the process graphs. So if you say to yourself, 'You know, I have this process; it's going to run and it's going to produce an artifact.' I have Process B that's going to produce another artifact. I have Process C that's waiting on those two artifacts and it's going to produce, you know, a third artifact. So now you can kind of imagine this graph—this acyclic graph where you have all these processes and whenever you know the conditions are met for your process, you can get started. And those conditions form a graph, and you often need to do all sorts of things with that graph, to say, 'You know, how long is this going to take?' Or or, you know, if this part failed, what needs to be regenerated?' Um, anytime you're doing anything with graphs, you're almost certainly using recursion because you want to see, you know, what are all the connections? Not just the first hop, but, you know, if this process fails, what are all the nodes that can't run?' And so it ends up fitting very nicely with recursion. What about you, Patrick? I'm kind of curious. You might have zero recursion or you might have a lot. I really don't know; we're going to find out.
B:So it was the thing I was bringing up. So I mean, a couple observations is: so one is not too much, but for Binary Search, it's a big one. So the sort of night way, but then as soon as you are like, 'Oh, I'm gonna do Binary Search,' I'll do recursion. You're like that bumped out. I'm doing it in a loop. Uh, so yeah, it is the recursive modeling in your mind, but doing it sort of like with index tracking, especially as Jason mentioned. If you only gonna if you only need to go one way sort of in the Fibonacci sequence, this happens. But in other stuff like Binary Search as well, like once you get to your end result—if that's tail recursion, right? Like basically if you get to your end result and all you're doing is returning up what you found, then to be to be fair, the recursive part unlike a sort of I'm computing some metrics on a graph and I need to preserve both and I need answers from two different paths and some computation and make some decisions is more complex. If all you're doing is looking for something in a tree and you're sort of—you each decision in the tree, you're only sort of having a singular sort of evaluation going on, and then you get to your end and you just pop pop pop pop pop pop pop, these ones become very suitable for moving into loops almost trivially. And in fact, some compilers and in some programming languages this is sort of like a first-class handled thing to do. So Binary Search—I've used it a fair amount to do that. Um, and then like you mentioned as well sort of sort of exploring graphs, but I would say actually exploring graphs is a model for a variety of things. So lots of things kind of turn into graphs depending on how you sort of do so some geometry problems where you sort of have like imagine a polygon and it has edges around the side. If you're doing traversals that can end up looking like graph traversals and have a similarity, but even when I'm mentioning you have an array that's sorted and you're doing Binary Search—even if you're doing a recursive, it ends up being a tree, right? You're treating the array as a tree in place. So it also becomes a form of basically.
B:Graph. You—it may not ever be that way, but it is a duality of the problem. It's just a different representation. And so in that way kind of the same things you're saying because I would say almost all problems that get handled this way have a lot of similarity to each other.
A:That makes sense when you um Um, in some of these languages like C++ is there like a memoization library that you can use or is there anything? I mean, is it mostly just coding it out of hash tables?
B:Yes. Uh, I mean this is one of the things I know in Python you can add a decorator, which I was blown away the first time I saw this to memoize, and I was just like, 'What? This is crazy.' Uh, this is voodoo. But uh yeah, I know in C++ is just yeah, mostly an unordered map and then making some structure for your function arguments and caching them. Uh yeah, you know the.
A:Thing about not wanting to go on too much of a tangent here, but like you know imagine if you have a function and it all it does is it does some work up front, it calls your it calls another function and then does some work afterwards. You can wrap—you can make that a decorator. So in Python, like a decorator can do just about anything. And so yeah, the decorators are extraordinarily powerful. We recently at work we created a decorator where you know, you do @ I don't remember what they called it, but you know, and what it will do is take the output of your function and before returning it will like store it in a database. And so you could do all sorts of really fun stuff with decorators. But under the hood, I think what Python takes advantage of is the fact that everything is hashable. I think with C++, how does oh yeah, I think with C++ there's like a something you have to implement, right? There's like an interface—interface like a hash type or something like that. Yeah, you need
B:to override if you want to insert it into a standard hash table. So I ask the either hashable or you have to provide a way of it being
A:hashed. Got it. Okay, but yeah, I guess just to kind of wrap that part of it up. So you know we talked about the Fibonacci example where you know you go to compute F of 98 and you see in your table that oh, the 98th element has something in it. So that means I've already computed that. What if your recursion is like the arguments are a lot more complicated? Right? Like what if it's—you know, a list of people's names that you've already looked at? That is the input to the recursive function. Well, you know, you have to have a way of saying, 'Oh, I've looked at this set of people already.' And so that means is ultimately hashing. You need to have some type of hash table, some type of one-way hash where you take the input regardless of how complicated it is, you turn it into a single number, and so then you can go back and look for that item again.
B:Before we finish our discussion of Fibonacci sequences, to save someone from writing us in. But Fibonacci sequence is like a very common—I don't know why it gets taught in CS4. Actually, I do. Is this like interesting property where it's associated with Phi? Where you divide sequential Fibonacci numbers and it is a closer and closer approximation of this numerical constant, Phi. So this like is very entertaining to people, but this is a broader part of the Lucas sequence—L-U-C-A-S, L-U-C-A-S—which is just all these recursive functions defined this way, and Fibonacci happens to be the one where it's like two consecutive and the first two numbers are one and one. But this is the one that gets talked about, but there's also really other fascinating properties and a lot of the other. I think I'm saying that right? Lucas numbers. I think there's like if you watch Number File YouTube, there's like a Number File video about this where they go into it—it's sort of one of those what is it? It's Tau versus Pi, right? Like Tau is superior instead of Pi. And anyways, it's like one of those like debates that get raging online. That's right. If someone's you're like, 'Oh, they're giving more credit to Fibonacci sequence here. Here's your shout out to the Lucas sequence.' Yeah.
A:There was somebody at work who is celebrating Tau Day, and it caused me to go down this internet rabbit hole where I don't know if I came out of it any smarter, but I learned a lot about Tau. Um so I
B:Guess I did like frantically Googling what they were talking
A:About now? Yeah. Um I you probably remember this, like Tau is some type of constant, like Pi is—it's
B:Two pi. It's just that's all it is. Oh, okay. It's twice Pi or Pi is half Tau? However you prefer to give precedent. Got.
A:It yeah, there's um oh man, we should definitely do a show. It's a little out of scope here. We should—I'd love to do a show on fractals and the whole like um you know the points on the space where the fractal goes to infinity and where it doesn't go to infinity. I'm totally drawing a blank on what is that thing called? It's like those those plots, those fractal plots, like
B:A Sierpinski gasket? Like what are you? No, it's
A:Mandelbrot set. That's what I was thinking of. So yeah, the Mandelbrot set is—you imagine if at every point on some grid you plotted like how long it takes for if you use that as an input, how long it takes for the output to reach infinity or whether it reaches infinity or not. Something like that. I think it's because oh, that's one thing we didn't really talk about is, you know, Fibonacci you know has a base case and so eventually you know it kind of results in some number. But you can imagine if you kind of went the other direction, it would just explode to infinity unless the numbers you're adding are also getting infinitely small. So if you add, you know, 1/2 + 1/3 + 1/4 + 1/5, you know the numbers you're adding are getting infinitely small, and so even though you're going to infinity, you're actually approaching a constant. It's kind of a weird.
B:Thing. Yeah, so the Mandelbrot set is you're taking the X and Y value of like a Cartesian plane and using them as inputs to complex numbers. And when you multiply complex numbers sometimes they end up converging and sometimes they don't. And so this is what like you're multiplying over and over in this sort of recursive relationship, and where it diverges very, very quickly, diverges slowly converges. And if you plot those out, you end up with a very surprisingly complex shape. Yeah, exactly.
A:Exactly. I think I don't
B:Know if this is helping anyone.
A:Well, we'll do a whole show on fractals. Fractals are really, really fun. We'll give our brain some time to rest from recursion and we'll do a fractal show in a few months. So yeah, I think you know maybe we could kind of walk through how to solve problems with recursion, kind of step-by-step way. So imagine you're in the Elite Code Competition of a lifetime or something, and you have—yeah, or even you're in your day job and you have to solve a problem with recursion. The first step, I always tell folks, is to write out the base case and make sure it's really comprehensive. So, one thing I'll see a lot of problems is where it's like okay, f(x - 10) + f(x - 1). Let's assume we have that kind of function. If I have that, then you know what's my base case for like negative six? Like what happens if I call f(4) and that causes me to call f(-6)? Is that okay? If I can't have f of negative numbers now, 4 needs to be a base case. Like I need to handle that in some special way. So, like make sure that there aren't any holes and that you always end up landing in one of these pockets where you're returning a constant or something that can be easily computed. And once you have the base cases and you've kind of looked at this and said oh, this is comprehensive, then you build the recursive step. And similar to what I said before when you're building that step, you it's a good time to make sure that you're not—you know that all of your base cases are covering all the ways you can call that. And it's very hard to do this; you have to rely a lot on intuition and just reading the equation carefully.
A:Then I would say, you know once you have that, then you could optionally add memoization. You know if you are doing things in a global context, like if you're reading from a file or if you're checking the internet or something, well, then you can't memoize because maybe you call f(4) twice, you get two different answers. And if that's by design, now you can't memoize. So ideally, you take whatever that external thing is that's causing your f(4) to be different and you—and you make that one of the inputs. So one of your inputs is, you know, the weather today or something like that. So you make it so it's I think the word is idempotent. You make it so that every time you call the function with these inputs, you get exactly the same output. Then you add memoization, and you should be good to go as far as testing recursive things. Patrick, you should definitely chat about how you test yours in your context. But in my case, I usually have a way of generating inputs and outputs. So most of the time if you're doing this like this example with trying to see how long our job is going to take, you can—if you for example the job is going to, you can say well, I have I want this job to take 60 seconds, so I want the answer to be 60. And I'm going to just divide. I'm going to put portions of this number 60 in my graph, so I'm going to have, you know, a chain in this graph that's going to add up to 60, and then I'm going to make a whole bunch of other chains that have like one e to the negative six time where I know that they're never going to take as long as that 60. And then I run my unit test, and I should get back 60. If I don't, then something has gone horribly wrong. And nine times out of ten when I write a test like
A:that it's actually is horribly wrong. So testing recursive stuff is really, really important.
B:Yeah, testing is important. I would say like I'll riff on it a bit and say like for me especially at least in the beginning, and with whatever your language contracts are for, which is basically adding checks for like what level of depth you're at—so keeping a counter and saying oh hey, I'm at level, you know, 100 or whatever. Is that something expected or something unexpected? And just adding something that gives yourself a nice helpful print statement or an assertion that says these things shouldn't be ever more than 100 or have more than so many results in your memoization map or whatever, right? And sort of adding some bounds checking at least initially just to make sure you don't get into that frustrating loop where you know you're computing thousands and thousands of things and it's just breaking and you can't kind of figure out why. So I think that's very helpful. Or Jason was kind of alluding to it before that. It doesn't do it for you, but as you've been developing for a while coming up with creative ways to visualize the like call graph—either you know in your debugger, which can still be pretty difficult, but like in your outputs, like having some ASCII pattern or something that tells you what level you're at or how the stuff is nested, or adding more spaces to the beginning. I've done various things. I think this can be really useful, so not in the unit testing context so much, but in the like—oh, something has gone wrong. Like how do I get it back under control? I think these kinds of things can be very helpful.
A:Yeah, that's a phenomenal, phenomenal point. Like in general, if you should write your program such that it never runs forever because that is the most painful thing to have to debug. And so especially if you're giving your program to someone else, like if this is a piece of software that people are going to install in their computer and it just runs forever and you know burns their battery or or just you know it keeps your computer from going to sleep, these are all things you should avoid. And so Patrick's exactly right. Every single recursion I've written in practice always has some type of check that says hey, you know, I've called this function, you know, 10,000 times. That can never happen. I never expect someone to have a workflow of 10,000 units long, and so I'm just going to abort.
B:I think where we get bit by that sometimes is people will do what they think is like a mathematical thing. So every so often will run across some computation that doesn't have a closed-form solution—that there isn't a direct way of solving it in a finite time. And so you'll use Newton's method to solve it, which is we didn't talk about this, but this is basically the same. We talk about it iteratively, but it is also somewhat recursively defined. And in fact, often people will define them recursively and you won't terminate because you'll end up in some state where you're oscillating between two values because of numerical stability and floating point or whatever. And for you're at a point. Yeah. And so like making sure that someone is keeping track of like we say how long, but often that can be expensive or difficult to do. But just like how many iterations you've gone and just set some like maximum iteration count that's reasonable. And if it didn't work either report that there was an error or just say look, you're close enough. Like just deal with it. But yeah, I think like making sure code, like Jason, I do occasionally write while true or forever, it always really scares me because I'm like this is really—I should just write while, you know, some counter is less than a million or something because it's really scary to write just a loop forever. Even ever however many years I've been programming, I still get scared to write like loops that don't have an auto-exit condition because you'll mess something up and it'll just run forever, and that's like a very frustrating thing for people after you bumping.
A:Into. Yeah, I mean, you know one practical example of this recently was I was writing some code that was interpreting JSON. So I had this JSON object, and it was—and the JSON object was recursive. You could have modules that had modules, so you could have objects that have objects. And but you know the recursion was pretty limited; like you'd never really go more than three levels deep. But I had a bug where, you know, I was reading my object and then instead of reading all the children objects, I read my own object oh no not many times for however many children there were, and it just never ended. And the worst thing too is the way because it blew up the call stack, it caused like my computer to act all weird, and you get these kind of weird things or your mouse slows down if you start blowing up the memory and everything. So yeah, just yet another data point for putting some type of constraint, like if you know you're never going to call us more than 10 times, you know, 10 levels deep, and just add one of your inputs, you know, how deep am I? And if it's, you know, 20 or something, you know that you're in trouble. Cool. All right. Well, that was recursion. You know, if you have any other questions, don't hesitate to leave comments, reply to us on X, or send us an email, and I'll be happy to continue the discussion over there. But yeah, I want to give a huge thanks to all of our patron Patreon patrons, all of our subscribers. Thank you so much for all of your support, and we'll see you all next time.
B:No, we'll see them in episode 163: Recursion. That's amazing. See you guys. Music by Eric Barndorler.
A:programming throwdown is distributed under a creative commons attribution share alike 2.0 license you're free to share copy distribute transmit the work to remix adapt the work but you must provide attribution uh to uh patrick and i and uh share alike in kind
Transcript supplied by the publisher with the episode.
Programming Throwdown
by Patrick Wheeler and Jason Gauci · English · Tech & Science
Programming Throwdown educates Computer Scientists and Software Engineers on a cavalcade of programming and tech topics. Every show will cover a new programming language, so listeners will be able to speak intelligently about any programming language.
More from Programming Throwdown
-
E166 · 16 Oct 2023 · 1 hr 12 min
166: Speedy Database Queries with Lukas Fittl
pganalyze: - Weekly series "5mins of Postgres": - How Postgres chooses which index to use: - CMU databases courses: - Postgres community: As well as social links: - Mastodon: - Twitter/X: @pganalyze, @LukasFittl - GitHub: @pganalyze, @lfittl - LinkedIn.
-
E165 · 25 Sep 2023 · 1 hr 17 min
165: Differential Equations
Patrick and Jason explain differential equations and why programmers should care about them. They cover rates of change, ordinary versus partial differential equations, numerical solvers, and practical examples ranging from simulations to PageRank and game physics.
-
E164 · 11 Sep 2023 · 1 hr 31 min
164: Choosing a Database For Your Project With Kris Zyp
Things to consider when choosing a database Speed & Latency Consistency, ACID Compliance Scalability Language support & Developer Experience Relational vs. NoSQL) Data types Security Database environment Client vs Server access Info on Kris & Harper: Website: harperdb.io Twitter: @harperdbio, @kriszyp Github: @HarperDB, @kriszyp.
-
E162 · 24 Jul 2023 · 1 hr 8 min
162: Interactive Fiction
In the latest episode of Programming Throwdown, we delve into the captivating world of interactive fiction. We explore: Wordnet, Inform, and how games in the past have been the forerunners of today’s NLP challenges.
-
E161 · 10 Jul 2023 · 1 hr 33 min
161: Leveraging Generative AI Models with Hagay Lupesko
MosaicML’s VP Of Engineering, Hagay Lupesko, joins us today to discuss generative AI! We talk about how to use existing models as well as ways to finetune these models to a particular task or domain.
-
E160 · 26 Jun 2023 · 1 hr 30 min
160: Position Localization
It’s a question that may seem easy to answer on the surface, but in truth hides more complexity than people expect. In today’s episode, we tackle the latest on AI, creative endeavors, and more before diving into the meaty discussion of position localization.
-
E189 · 24 Aug 2026 · 1 hr 23 min
189: Agentic Loops
-
E188 · 9 Jul 2026 · 1 hr 36 min
188: World Models
-
E187 · 2 May 2026 · 1 hr 38 min
187: Agentic Coding
-
E186 · 3 Feb 2026 · 1 hr 28 min
186: Becoming a Manager
Patrick and Jason discuss what it means to become a manager and how the role differs from individual engineering work. They cover hiring, coaching, performance management, team goals, and when moving into management is or is not the right choice.
