![]() |
![]() |
![]() |
![]() |
![]() |
|
Next, note that you can arrange for recursivePowerOfTwo to hand
over a little less work to powerOfTwo by performing one of the
multiplications by 2 in recursivePowerOfTwo itself, and subtracting
1 from powerOfTwo's argument:
Integer method definition instance recursivePowerOfTwo self = 0 ifTrue: [^ 1] ifFalse: [^ 2 * (self - 1) powerOfTwo] Workspace Transcript show: 4 recursivePowerOfTwo printString; cr Transcript 16
Clearly, recursivePowerOfTwo must work as long as one of the following
two situations holds:
self is 0; in this situation,
the recursivePowerOfTwo answers 1.
self is not 0, but the powerOfTwo
method is able to compute the power of 2 that is 1 less than the value
of self.