13

That feeling when recursion finally "clicks", and you begin instantly identifying candidates in your code for recursive solutions.

Comments
  • 0
    @Artemix Why "worst solution possible"? I find recursive code to be quite elegant.

    Yeah, there is a danger of a stack overflow, but if you're sure of the inputs that will go into the method, it can be quite a powerful way to solve problems.
  • 0
    @Artemix Good point. Anyway, it's still useful for solving a certain class of problems. As long as we know exactly where it works well, and keep its drawbacks in mind, it's just another tool we can employ.
  • 2
    @vinaysshenoy Indeed. When the only tool you have is a hammer, everything looks like a nail. ;)
  • 2
    Please do not implement recursion everywhere. Let me give you an exercise: try to write a recursolution for something where you need more than 10,000 loops/instances/whatever/you know what i mean. Your solution will never work nicely, because: maximum recursion depth. Loops handle that shit with like foo bar.
  • 1
    @darksideplease Yeah, I get that. My loop will run 2-3 rounds at max. As I said, I know exactly what's going into the function.
  • 1
    Properly understanding recursion was a big achievement for me
  • 0
    @vinaysshenoy recursive funcions are also often risky, as can be used by a malicious user to break your system. Like, if you have a method to find the min between n numbers an that is recursive, at the lowest grade you have n method headers in your memory. Make n big and you can get a stack overflow.
  • 1
    Like everyone else had said, be careful with recursion.
  • 1
    Like everyone else had said, be careful with recursion.
  • 1
    Like everyone else had said, be careful with recursion.
  • 1
    Like everyone else had said, be careful with recursion.
  • 3
    Like everyone else had said, be careful with recur

    EXCEPTION: stack overflow
  • 1
    To the people who said recursion is bad:

    We have been far too polluted with the idea of loops and mutations with the Algol family of languages.

    Elegant programming languages like Lisp don't even have any looping consutucts.

    The Algol family became popular because the machines were not powerful enough and it was much closer to assembly (in terms of adaptation). We are not in that era anymore and the machines are powerful enough to handle languages like Haskell lisp.

    I can confidently say that recursive solutions are the most intuitive way to solve problems relative to their looping counterparts. Lot of popular languages internally does optimizations to handle very large recrusive calls. This opinion is from personal realization and conversations with experienced programmers who have delved deep into both family of languages
  • 0
    @Artemix I disagree.

    Recursion forces the user to think about the smallest problem first.

    It easily offers more code readability and can solve complex problems in minimal readable lines.

    Coming to the performance, the optimizations easily improve the performance. Recursion does not always mean huge function call stacks, a lot of optimization basically evaluates it to be a simple expression.
  • 0
    @frozentruth dude, what did you smoke? Recursion often makes the code LESS readable. Also it is almost always in practice not useful when iterating over a big list of something, because - maximum recursion depth. And just setting the recursion depth number higher is not a practicable, yet future proof solution.
  • 1
    Oh, you make me feel younger again. :D
  • 0
    @darksideplease I don't smoke xD
    Let me use your own example. Consider a big ass list that you want to transform. Modern FP languages will beat the crap out of iterative solutions. Imagine the code you have to write to make it concurrent in the iterative approach.

    Coming to the readability part, take this with a pinch of salt. The understanding of recursion is a journey. Unless people have been through that journey (for ex: try solving simple problems with scheme), the beauty of recursion is never realized.
  • 0
    @frozentruth beautiful yes. And yeah if you code in scheme or lisp then i agree. But not for the commonly used languages, eg java, php, c[\+#]*, python, ...
  • 0
    @Artemix me too. Festival (what i'm currently testing) uses scheme as a scripting interface..
  • 0
    @Artemix @darksideplease lol Lisp was used as an example. The points that I am trying to make is really evident in lisp. just use the example of Clojure instead.
    Also speaking about real world, WhatsApp was built using Erlang. Facebook spam filter uses Haskell.

    Imagine the use of map reduce in the real world systems.
Add Comment