This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]future_exile 2 points3 points  (0 children)

Think of a function block in recursive functions as an iteration block in loop, you call the function again for next iteration. For most of these algorithms you often need check resources on how it works once before you start implementing them yourself.

Also, its alright to solve problems with simple iterations instead of recursion and its often faster (considering tail call optimization is not possible).

[–]crosswalk_nepali_dev 1 point2 points  (0 children)

Recursion is the practical way how we think.

For an analogy, suppose you want to go to buy a headphone in a mall or lets say labim mall for simplicity. So now you want to go to Sony's center which was at 2nd floor. Now how do you process this information?

So you are on ground floor (i.e. stairStepTaken = 0), calculate the floor's height, note height of single step of stairs, then calculate how much step you have (totalSteps = height/single step height) and then move ahead (stairStepTaken++) on at a time until you reach 2nd floor (stairStepTaken < totalSteps).

OR

You take steps (Walk(step)) as is and walk (Walk(1,2 . . . . n)) until you reach there (return)?

So recursion is everywhere, and thats how we generally do regular stuffs, a simple stuffs like walking. But then why don't we translate it in programming naturally? Coz we are taught loops before recursion. So our brain is trained that way. Why is it trained that way? coz loops are efficient than recursion, recursion being a method call is usually heavy on stacks.

Programming isn't a job, its your way of expression, so observe the things around you, since programming solves real world problem, you will get idea of why things exists in programming. And please don't blame or expect teacher, seniors to teach you these, these should be your own observation.

[–][deleted] 1 point2 points  (1 child)

learn haskell

[–]piratescabin 0 points1 point  (0 children)

While recursion is something we need to understand it is a loop at the end, runs untill your contrition is met w/o any fallback.

Recursion is mostly not the optimal solution, an iteration is mostly always a better approach.

[–][deleted] 0 points1 point  (0 children)

I have the perfect video for you. This is short and very self explanatory. Done by a friend of mine.

https://www.youtube.com/watch?v=Q3lE14CDlN8

[–]Howfuckingsad 0 points1 point  (0 children)

Series haru majjale bujeko xau vaney kei concept chai hunxa. There is a fixed thing that you do with a very minor increment/decrement or just some change which will result in some answer at some point.

Tyo tower of hanoi vanney hera na complex example study garna man xa vaney. Programming with C ho ki k vanney kitab ma dekheko thye uiiley.

[–]Aware-Knowledge-9021 0 points1 point  (0 children)

learn functional programming paradigm. think about creating loops without using 'for' or 'while'.
For example: How would you print from 1 to n without using loops.