# Labels *Table of contents* ## Exceptions and hash tables *(unfinished)* ## Mutually recursive functions Suppose I want to define two functions which call each other. This is actually not a very common thing to do, but it can be useful sometimes. Here's a contrived example (thanks to Ryan Tarpine): The number 0 is even. Other numbers greater than 0 are even if their predecessor is odd. Hence: ```ocamltop let rec even n = match n with | 0 -> true | x -> odd (x-1) ``` The code above doesn't compile because we haven't defined the function `odd` yet! That's easy though. Zero is not odd, and other numbers greater than 0 are odd if their predecessor is even. So to make this complete we need that function too: ```ocamltop let rec even n = match n with | 0 -> true | x -> odd (x-1) let rec odd n = match n with | 0 -> false | x -> even (x-1) ``` The only problem is... this program doesn't compile. In order to compile the `even` function, we already need the definition of `odd`, and to compile `odd` we need the definition of `even`. So swapping the two definitions around won't help either. There are no "forward prototypes" in OCaml but there is a special syntax for defining a set of two or more mutually recursive functions, like `odd` and `even`: ```ocamltop let rec even n = match n with | 0 -> true | x -> odd (x-1) and odd n = match n with | 0 -> false | x -> even (x-1) ``` It's hard to know exactly how useful this is in practice, since I've never had cause to write mutually recursive functions, nor have I been able to think of a non-trivial example. However it's there. You can also use similar syntax for writing mutually recursive class definitions and modules. ## Aliases for function names and arguments Recall in Chapter 5 we talked about partial function application. It's possible to use this as a neat trick to save typing: aliasing function names, and function arguments. Although we haven't looked at object-oriented programming (that's the subject for the next chapter), here's an example from OCamlNet of an aliased function call. All you need to know is that `cgi # output # output_string "string"` is a method call, similar to `cgi.output().output_string ("string")` in Java. ```ocaml let begin_page cgi title = let out = cgi # output # output_string in out "\n"; out "\n"; out ("" ^ text title ^ "\n"); out ("\n"; out "\n"; out "\n"; out ("