Home Segments Index Top Previous Next

315: Mainline

Once you see that you can define recursivePowerOfTwo in terms of powerOfTwo, you are ready to learn how to turn recursivePowerOfTwo into a recursive method that does not rely on powerOfTwo.

First, note that you can eliminate the need to call powerOfTwo in the simple case in which the value of recursivePowerOfTwo's receiver is 0:

Integer method definition • instance 
recursivePowerOfTwo 
  self = 0 
    ifTrue: [^ 1] 
    ifFalse: [^ self powerOfTwo] 
Workspace
Transcript show: 4 recursivePowerOfTwo printString; cr  
Transcript 
16