Why should recursion be preferred over iteration?

前端 未结 18 1192
既然无缘
既然无缘 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:44

    Try implementing depth-first search recursively and iteratively and tell me which one gave you an easier time of it. Or merge sort. For a lot of problems it comes down to explicitly maintaining your own stack vs. leaving your data on the function stack.

    I can't speak to Haskell as I've never used it, but this is to address the more general part of the question posed in your title.

提交回复
热议问题