Writing correct recursive definitions of functionsTutorial (draft)WarningMutually recursive definitionsDocumentation and user's manualTable of contentsOCaml programs

Mutually recursive definitions

   
  This code gives a correct definition of both the predicate functions checking whether a natural number is even and whether a natural number is odd

let rec
  is_even = function
    n -> (* n >= 0 *)
         if n = 0
           then true             (* basis *)
           else is_odd (n - 1)   (* induction *)
  and
  is_odd = function
    n -> (* n >= 0 *)
         if n = 0
           then false            (* basis *)
           else is_even (n - 1)  (* induction *)

This is a mutually recursive definition.

   
  Similarly mutually recursive definitions of types can be written.


Latest update : October 5, 2006
This document was translated from LaTeX by Hyperlatex 2.5, which is not the latest version of Hyperlatex.

Writing correct recursive definitions of functionsTutorial (draft)WarningMutually recursive definitionsDocumentation and user's manualTable of contentsOCaml programs