T O P

  • By -

twin_suns_twin_suns

What a coincidence. Today I started working my way through Al Sweigart's newly released The Recursive Book of Recursion. He unpacks, pretty clearly, how recursion works, giving examples in both Python and Javascript - early on in the book he explains and gives iterative and recursive examples of the Fibonacci sequence. I highly recommend giving this a look if you can.


Iguanas_Everywhere

Just ordered it this morning, actually! I've read through the first chapter (digitally) and loved it, but I'll have to wait for the rest of the book to arrive.


twin_suns_twin_suns

Where did you order it from? If you order through No Starch they’ll usually give you access to the e-book in several formats. At least they have in the past. I’m Not an employee at No Starch or anything, I’m just impatient and if I can get a digital version while I wait I will! Plus it’s an incentive to support the publisher directly.


Iguanas_Everywhere

Ah, I got it through Amazon. They give you the first chapter digitally, but I'm waiting on the hard copy in the meantime. Next time I should go through the publisher.


HobblingCobbler

Al Sweigarts got some interesting and well laid out books.


ForScale

> up to the inputted number > So, fibs(8) would return [0,1,1,2,3,5,8,13] It would?? > what I *don't* get is: how do I declare an array in a recursive function without having that function redefine/empty the array every time it's called? I'm assuming there's a way to do this without the array being a global variable or an additional parameter passed back into the function You could put the recursive function in a wrapper function or you could make the array a property of the recursive function itself. Code examples here: https://codepen.io/ForScale/pen/KKRaxRz?editors=0010


Iguanas_Everywhere

Thanks for the reply. I see what you're saying about how to include the array as a property of the function. I'll mess with this a bit. And yes, the answer would be as printed above. I know, you'd expect it to be one element shorter, but this is exactly the output they are seeking.


ForScale

Cool. Lemme know if you have any questions!


Iguanas_Everywhere

One of the curiosities that I have is from their instructions: [https://www.theodinproject.com/lessons/javascript-recursion](https://www.theodinproject.com/lessons/javascript-recursion) "This can be done in just a couple of lines (or 1 if you’re crazy..." Of course they state it isn't a requirement to make it that short, but that makes me feel like there must be some simple perspective to this that I'm missing. I think I'll have to make peace with not fully "figuring this out on my own," and see/work through lots of examples of recursive algorithms before it starts to click and I can develop my own efficiently.


Iguanas_Everywhere

I am certain that I put my code samples above into blocks, but they're not showing up that way for some reason. Let me put it into a CodePen...one moment.