Home Segments Index Top Previous Next

328: Mainline

Now, to illustrate the concept of mutually recursive methods, suspend disbelief, and suppose that you have decided that the rabbits method is too big. Accordingly, you rewrite the rabbits method in terms of two auxiliary methods:

Integer method definition • instance 
rabbits 
  (self = 0 or: [self = 1]) 
    ifTrue: [^ 1] 
    ifFalse: [^ self previousMonth + self penultimateMonth] 

Realizing that previousMonth must answer the number of rabbits at the end of the previous month, you see that you can define previousMonth as follows:

Integer method definition • instance 
previousMonth 
  ^ (self - 1) rabbits 

Analogous reasoning leads you to the following definition for penultimateMonth:

Integer method definition • instance 
penultimateMonth 
  ^ (self - 2) rabbits