teh_pwn said:
Don't know why your teacher is asking you to do that with recursion...terribly inefficient. A for loop would do, and there might even be a way to do it with bit shifting...
Scheme is a functional language. Recursion is, if anything, the normal way of doing most operations in most functional languages.
A way of doing it that would be ok for positive or negative would be to, rather than use -1, use -(n/abs
). But that would almost certainly be slower unless n is a constant and the function is generated properly to indicate that. Not sure how well scheme deals with such things.
Recursion is not inherently bad, it just requires a lot more work on the part of the compiler in order to optimize it. C compilers tend to be very simplistic, and the language itself is not well designed for recursion. So the compilers don't tend to go that far in optimization.
Languages like scheme are designed for recursion, and the compilers know it's an area they *have* to be able to optimize well.