Why should recursion be preferred over iteration?

前端 未结 18 1214
既然无缘
既然无缘 2020-11-29 18:50

Iteration is more performant than recursion, right? Then why do some people opine that recursion is better (more elegant, in their words) than iteration? I really don\'t see

18条回答
  •  甜味超标
    2020-11-29 19:43

    Several things:

    1. Iteration is not necessarily faster
    2. Root of all evil: encouraging something just because it might be moderately faster is premature; there are other considerations.
    3. Recursion often much more succinctly and clearly communicates your intent
    4. By eschewing mutable state generally, functional programming languages are easier to reason about and debug, and recursion is an example of this.
    5. Recursion takes more memory than iteration.

提交回复
热议问题