I've been running through The Little Schemer, and have hit the example of Peano multiplication. The solution is given in TLS (reproduced below) -- however what interests me is the order of the algorithm.
(define mX
(lambda (n m)
(cond
((zero? m) 0)
(else
(+ n (mX n (1- m))))
)))
My understanding is that this sort of operation is to be avoided, as the order of the operation is unnecessarily large.
To that end I've been trying to think of how to convert this to a linear operation (I was able to do so in the case of addition). Nesting two functions occurred to me, but once i do this i am unsure how to measure the order of the function.
(define (mX n m)
(define (mX-aux n m product)
(if (zero? m)
product
(mX-aux n (1- m) (+ product n))))
(mX-aux n m 0))
Is this approach linear or recursive? Is there a another way to do this? In particular, is it possible to do this without setting variables?