Mutual Recursion - Example

Example

For instance, consider two functions even? and odd? defined as follows:

function even?(number : Integer) if number == 0 then return true else return odd?(abs(number)-1) function odd?(number : Integer) if number == 0 then return false else return even?(abs(number)-1)

These functions are based on the realization that the question is 3 even? is equivalent to is 2 odd?, which is equivalent to is 1 even?, which is equivalent to is 0 odd?; and the answer to that is defined as false. The abs function ensures that number is positive, so that subtracting 1 every time is guaranteed to lead towards 0, not away from it.

Read more about this topic:  Mutual Recursion

Famous quotes containing the word example:

    Our intellect is not the most subtle, the most powerful, the most appropriate, instrument for revealing the truth. It is life that, little by little, example by example, permits us to see that what is most important to our heart, or to our mind, is learned not by reasoning but through other agencies. Then it is that the intellect, observing their superiority, abdicates its control to them upon reasoned grounds and agrees to become their collaborator and lackey.
    Marcel Proust (1871–1922)