3 ms·
I don't think the example I've given is iterative. It generates a recursive process. You're right on a different point though, this example is much more effici
by jinfiesto 16y ago
I don't think the example I've given is iterative. It generates a recursive process.
You're right on a different point though, this example is much more efficiently implemented iteratively. The point was to illustrate that writing recursive functions is not difficult.
I guess we don't see eye to eye on recursion being taught first. :p In my opinion, recursions analog to "the real world" is much more intuitively obvious than iteration.
- fferen 16y agoI think parent was referring to the definition on the Wikipedia page: "When n is a positive integer, exponentiation corresponds to repeated multiplication." And then an equation that suggests an iterative approach.
- colomon 16y agoExactly. Good catch.
- charlieflowers 16y agoActually, I think your example generates an iterative process, not a recursive process. In other words, according to what I read in SICP recently (working through it the first time), I think your example is a recursive function that generates an iterative process. It's an iterative process because incoming function arguments capture a snapshot of its state at any point, and therefore you don't need state that exists deeper on the call stack. The upshot is that iterative processes are more efficient in terms of how much stack space they use. Not trying to nitpick -- I enjoyed your post and it is still relevant. Also, I could be wrong, so someone please correct me if so.