Hacker News new | ask | show | jobs
by akashakya 2913 days ago
> Loops are closer to what CPU actually does. A compiler is therefore simpler to implement and will likely contain less surprises.

I dont find the argument convincing. By that assembly is closer to what CPU actually does, why not write code using goto (or binary)? We all can agree on its easy to write reasonably good code high-level languages. I think it because high-level languages are closer to the problem domain (or our thinking), hence it's easier to define the solution in these languages. same for recursion.

> It’s easier to use a debugger on procedural-style or OO code (step in/over/out, local variables) compared to recursive functional style advertised by SICP.

I feel exactly opposite. OO by definition have state bound to object outside the method (not actually local variable), so you have to watch all the objects method is affecting. Not pleasant experience when you have hundreds of objects. A recursive code on the other hand usually depends only on the local arguments so you can only focus on current step.

> In most runtimes, call stack is limited to a small size, a couple of megabytes. Stack overflow exception is hard to handle in a reasonable way. When instead using a loop with a stack container for the state, it can be much larger, also easier to handle overflows i.e. implement soft and hard limits.

Well, depends on the platform you are working on. For desktop at least it's definitely not "a couple of megabytes".

I feel that most of the time choice is between readability and performance. Sometimes maybe the solution is inherently iterative/recursive making choice easier. ymmv though.

1 comments

> I think it because high-level languages are closer to the problem domain (or our thinking)

Mostly agree.

> same for recursion

I don’t know about you, but in my problem domains loops are closer then recursion. Also loops are closer to my thinking. YMMV.

> Not pleasant experience when you have hundreds of objects

No one forces you to design your OO apps this way. When you only have a few objects, or when you only process them sequentially, there’s no need in complex OO model, a functional or procedural code will do fine.

However, there’re apps where there’re hundreds of interacting objects, such as a web browser.

> For desktop at least it's definitely not "a couple of megabytes".

On Windows it’s usually (=for apps linked with default settings) 1 megabyte.

If you are dealing with something other than a sequence, such as a tree or a more general network, recursion is often the more insightful way to think about a solution.

Actually, the simplest example I can think of where this the case involves a sequence: binary search.

On the other hand, if you have to significantly rework the conceptual recursive solution to put it in tail-recursive form, an implementation using loops may end up being easier to understand.