Mutual recursion: Difference between revisions

Content deleted Content added
No edit summary
Updated both even and odd to deal with the case where number starts off as negative
Line 7:
'''return''' true
'''else'''
'''return''' odd?(abs(number)-1)
'''function''' ''odd?''(number : '''Integer''')
Line 13:
'''return''' false
'''else'''
'''return''' even?(abs(number)-1)
 
These functions are based on the realization that the question ''is three even'' is equivalent to the question, ''is two odd'', which is the same as asking if 1 is even or 0 is odd. In the end, the answer is no, as realized by the function <code>odd?</code>. The <code>abs</code> function is used to ensure that <code>number>/code> decrements towards zero even when it starts off as a negative value.
 
Mutual recursion is very common in the [[functional programming]] style, and is often used for programs written in [[Lisp programming language|LISP]], [[Scheme (programming language)|Scheme]], [[ML programming language|ML]], and similar [[programming language|languages]]. In languages such as [[Prolog programming language|Prolog]], mutual recursion is almost unavoidable.